-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
56 lines (40 loc) · 1.34 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
var express = require('express');
var bodyParser = require('body-parser');
var MongoClient = require('mongodb').MongoClient;
var app = express();
var mongoLikeForRealz;
app.use(require('connect-livereload')({
port: 35729
}));
// Connection URL
var url = 'mongodb://localhost:27017/play';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
console.log('Connected correctly to server');
mongoLikeForRealz = db;
//db.close();
});
app.use(express.static('public'));
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.get('/users', function (req, res) {
console.log('get', req.query);
var collection = mongoLikeForRealz.collection('users');
collection.find({}).toArray(function(err, docs) {
res.send(docs);
});
});
app.post('/users', function (req, res) {
console.log('post', req.body);
var collection = mongoLikeForRealz.collection('users');
collection.insert(req.body, function (err, result) {
if (err)
return res.status(500).send('sorry');
res.send(result);
});
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});