-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
executable file
·50 lines (40 loc) · 1.18 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
#!/usr/bin/env node
var express = require('express');
var mongo = require('mongoskin');
var PORT = 5000;
var DB = 'localhost:27017/msgdb'
var COLL = 'messages'
var app = express();
app.configure(function() {
app.use(express.logger()); // Logging middleware
app.use(express.bodyParser()); // Used to parse body from post request
app.use(express.static(__dirname+'/static')) // Normally we would use a http server like Nginx to server static files but this is fine for now.
});
msgColl = mongo.db(DB, { safe: true }).collection(COLL)
app.get('/messages.json', function(req, res) {
msgColl.find({}, { sort: { date: -1 }, limit: 20 }).toArray(function(err, items) {
if(err) {
return res.send(err);
}
res.json(items);
});
});
app.post('/messages', function(req, res) {
body = req.body;
console.log(body);
msg = body.msg; date = body.date;
if( msg && date ) {
msg = {
msg: msg,
date: +date
}
msgColl.insert(msg, {}, function() {
res.send("Inserted message!");
});
} else {
res.send("Invalid message");
}
});
app.listen(PORT, function() {
console.log("Listening on " + PORT);
});