-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgood.js
40 lines (32 loc) · 883 Bytes
/
good.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
var express = require('express');
var mongodb = require('mongodb');
var logger = require('./logger.js');
var app = express();
var MONGODB_URI = 'mongodb://localhost';
var db;
var coll;
// Initialize connection once, reuse the database object
mongodb.MongoClient.connect(MONGODB_URI, { server: { logger: logger(MONGODB_URI) } }, function(err, database) {
if(err) throw err;
db = database;
coll = db.collection('test');
app.listen(3000);
console.log('Listening on port 3000');
});
app.get('/', function(req, res) {
coll.find({}, function(err, docs) {
docs.each(function(err, doc) {
if(doc) {
res.write(JSON.stringify(doc) + "\n");
}
else {
res.end();
}
});
});
});
app.get('/post', function(req, res) {
coll.insert({ randomNumber: Math.random() }, function(err) {
res.end('Successful Insert!');
})
});