forked from abhilashmk/nodejs-mongodb-atlas
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
48 lines (39 loc) · 1.15 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const express = require('express');
const mongodb = require('mongodb');
const dns = require('dns');
var app = express();
app.get('/', function (req, res) {
res.send("Hello " + (req.query.name || 'Anyone'));
});
app.get('/connect-short', function (req, res) {
var uri = process.env.MONGO_DB_CONNECTION_SHORT;
connect(req, res, uri);
});
app.get('/connect-long', function (req, res) {
var uri = process.env.MONGO_DB_CONNECTION_LONG;
connect(req, res, uri);
});
app.get('/dns', (req, res) => {
const hostname = req.query.hostname;
dns.resolveSrv(hostname, function (e, address) {
if (e) {
res.json(e);
} else {
res.json(address);
}
});
})
function connect(req, res, uri) {
mongodb.connect(uri, { useNewUrlParser: true }, function (err, db) {
if (err) {
res.json(err);
} else {
db.close();
res.send("Connected success");
}
});
}
var port = process.env.PORT || 1337;
var server = app.listen(port, function () {
});
console.log("Server running at http://localhost:%d", port);