-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
53 lines (48 loc) · 1.13 KB
/
Copy pathapi.js
File metadata and controls
53 lines (48 loc) · 1.13 KB
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
var express = require('express'),
Bourne = require('bourne'),
bodyParser = require('body-parser'),
db = new Bourne('data.json'),
router = express.Router();
router
.use(function (req, res, next) {
if(!req.user) req.user = { id: 1 };
next();
})
.use(bodyParser.json())
.route('/contact')
.get(function(req,res) {
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)}
})
.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;