-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
51 lines (46 loc) · 1.42 KB
/
api.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
var express = require('express'),
Bourne = require('bourne'),
bodyParser = require('body-parser'),
db = new Bourne('data.json'),
router = express.Router();
router
.use(bodyParser.json())
.route('/contact')
.get(function (req, res) {
req.user = {'id':1};
db.find({ userId: parseInt(req.user.id, 10) }, function (err, data) {
res.json(data);
});
})
.post(function (req, res) {
var contact = req.body;
contact.userId = req.user.id;
db.insert(contact, function (err, data) {
res.json(data);
});
});
router
.param('id', function (req, res, next) {
req.dbQuery = { id: parseInt(req.params.id, 10) };
next();
})
.route('/contact/:id')
.get(function (req, res) {
db.findOne(req.dbQuery, function (err, data) {
res.json(data);
});
})
.put(function (req, res) {
var contact = req.body;
delete contact.$promise;
delete contact.$resolved;
db.update(req.dbQuery, contact, function (err, data) {
res.json(data[0]);
});
})
.delete(function (req, res) {
db.delete(req.dbQuery, function () {
res.json(null);
});
});
module.exports = router;