-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
65 lines (55 loc) · 1.3 KB
/
api.js
File metadata and controls
65 lines (55 loc) · 1.3 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
54
55
56
57
58
59
60
61
62
63
64
65
var express = require('express'),
Bourne = require('bourne'),
bodyParser = require('body-parser'),
router = express.Router(),
db = new Bourne('data.json');
/* Hacking validation
.use(function (req, res, next) {
if (!req.user) req.user = {id: 1};
next();
})
*/
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)};
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.$resolve;
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;