-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (51 loc) · 1.75 KB
/
index.js
File metadata and controls
68 lines (51 loc) · 1.75 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
66
67
68
var restify = require('restify');
var citySave = require('save')('city');
var server = restify.createServer({
name: 'my-api'
});
server.listen((process.env.PORT || 5000), function() {
console.log('%s listening at %s', server.name, server.url);
});
server
.use(restify.fullResponse())
.use(restify.bodyParser());
server.get('/cities', function (req, res, next) {
citySave.find({}, function (error, cities) {
res.send(cities)
})
});
server.post('/cities', function (req, res, next) {
if (req.params.name === undefined) {
return next(new restify.InvalidArgumentError('Name must be supplied'))
}
//citySave.create({ name: req.params.name }, function (error, city) {
citySave.create(req.params, function (error, city) {
if (error) return next(new restify.InvalidArgumentError(JSON.stringify(error.errors)))
res.send(201, city)
})
})
server.get('/cities/:id', function (req, res, next) {
citySave.findOne({ _id: req.params.id }, function (error, city) {
if (error) return next(new restify.InvalidArgumentError(JSON.stringify(error.errors)))
if (city) {
res.send(city)
} else {
res.send(404)
}
});
});
server.put('/cities/:id', function (req, res, next) {
if (req.params.name === undefined) {
return next(new restify.InvalidArgumentError('Name must be supplied'))
}
citySave.update({ _id: req.params.id, name: req.params.name }, function (error, city) {
if (error) return next(new restify.InvalidArgumentError(JSON.stringify(error.errors)))
res.send()
});
});
server.del('/cities/:id', function (req, res, next) {
citySave.delete(req.params.id, function (error, city) {
if (error) return next(new restify.InvalidArgumentError(JSON.stringify(error.errors)))
res.send()
});
});