-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (51 loc) · 2.08 KB
/
app.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var MongoClient = require('mongodb').MongoClient , assert = require('assert');
var express = require('express'); // call express
var app = express(); // define our app using express
//var bodyParser = require('body-parser');
// Connection URL
var mongoAtlasURL = 'mongodb://chris:hZVJq2qoSkSu6Twe@cluster0-shard-00-00-9gjqg.mongodb.net:27017,cluster0-shard-00-01-9gjqg.mongodb.net:27017,cluster0-shard-00-02-9gjqg.mongodb.net:27017/sample?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin'
var findDocuments = function(db,fil, callback) {
// Get the documents collection
var collection = db.collection('peoples');
// Find some documents with the Filter {"first_name": "Ye"}
collection.find(fil).toArray(function(err, docs) {
assert.equal(err, null);
mongoAtlasDocument = docs;
console.log("Found " + docs.length+ " records at: " + collection.collectionName);
console.log(docs)
callback(docs);
});
}
// Use connect method to connect to the server
MongoClient.connect(mongoAtlasURL, function(err, db) {
assert.equal(null, err);
mongoAtlasDB = db;
console.log("Connected successfully to Atlas server");
console.log("Connected successfully to database: " + db.databaseName);
var server = app.listen(process.env.PORT || 8080, function () {
var port = server.address().port;
console.log("App now running on port", port);
});
});
// GET method route
app.get("/people", function(req, res) {
var collection = mongoAtlasDB.collection('peoples');
// Find some documents with the Filter {"first_name": "Ye"}
collection.find({}).toArray(function(err, docs) {
assert.equal(err, null);
mongoAtlasDocument = docs;
console.log("Found " + docs.length+ " records at: " + collection.collectionName);
console.log(docs);
res.json(docs);
});
});
// GET method route
app.get("/people/:first_name", function(req, res) {
var filter = {"first_name": req.params.first_name}
findDocuments(mongoAtlasDB,filter,function(docs){
res.json(docs);
});
});
// POST method rout
app.post("/contacts", function(req, res) {
});