-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·47 lines (35 loc) · 1.19 KB
/
server.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
var express = require('express');
var app = express();
var MongoClient = require('mongodb').MongoClient;
// the name "mongo" comes from the docker link, in the docker-compose.yml
var url = 'mongodb://localhost:27017/dockerdemo';
var db;
MongoClient.connect(url, function (err, database) {
if(err){ console.log('failed to connect: ' + err); return;}
db = database;
console.log("Connected correctly to server!!");
});
app.get('/', function(req, res){
res.send('Greetings from the server!!!');
});
app.get('/createMongo', function(req, res){
var name = 'user' + Math.floor(Math.random()*10000);
var email = name + '@mit.edu';
var collection = db.collection('customers');
var doc = {'name':name, 'email':email};
collection.insert(doc, {w:1}, function(err, result) {
console.dir(result);
res.send(result);
});
});
app.get('/readMongo', function(req, res){
var results = null;
var collection = db
.collection('customers')
.find()
.toArray(function(err, docs) {
console.dir(docs);
res.send(docs);
});
});
app.listen(3000);