-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
64 lines (52 loc) · 1.36 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
var express = require('express')
var morgan = require('morgan')
var bodyParser = require('body-parser')
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/redux-debug');
var cors = require('cors')
var app = express()
app.use(morgan('dev'))
app.options('*', cors());
app.use(function(req,res,next){
req.db = db;
next();
});
app.use(bodyParser.json({limit: '10mb'}));
app.use(bodyParser.urlencoded({ extended: false }));
app.post('/log', function (req, res, next){
var db = req.db;
var log = {
data: req.body.log,
submittedBy: req.body.submittedBy,
submittedAt: (new Date).toISOString()
}
// Set our collection
var collection = db.get('logs');
// Submit to the DB
var promise = collection.insert(log)
promise.on('error', function(err){
console.log(err);
res.sendStatus(500)
});
promise.on('success', function(doc){
res.json(doc)
});
})
app.get('/log/:id', function (req, res, next){
var id = req.params.id
var promise = req.db.get('logs').findById(id)
promise.on('error', function(err){
console.log(err);
res.sendStatus(500)
});
promise.on('success', function(doc){
if (!doc) return res.sendStatus(404);
res.json(doc)
});
})
app.use(function(req, res, next){
res.sendStatus(404)
})
var PORT = 8000
app.listen(PORT, function () {"listening on " + PORT})