-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·66 lines (51 loc) · 1.36 KB
/
Copy pathserver.js
File metadata and controls
executable file
·66 lines (51 loc) · 1.36 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
var express = require('express'),
http = require('http'),
items = require('./data/menu-items');
var app = express()
.use(express.bodyParser())
.use(express.static('public'));
app.get('/items', function (req, res) {
res.json(items);
});
app.post('/items', function (req, res) {
var matches = items.filter(function (item) {
return item.url === req.body.url;
});
if (matches.length > 0) {
res.json(409, {status: 'item already exists'});
} else {
req.body.id = req.body.url;
items.push(req.body);
res.json(req.body);
}
});
app.get('/items/:item_name', function (req, res) {
var matches = items.filter(function (item) {
return item.url === req.params.item_name;
});
if (matches.length > 0) {
res.json(matches[0]);
} else {
res.json(404, {status: 'invalid menu item'});
}
});
app.delete('/items/:item_name', function (req, res) {
var found = false;
items.forEach(function (item, index) {
if (item.url === req.params.item_name) {
found = index;
}
});
if (found) {
items.splice(found, 1);
res.json(200, {status: 'deleted'});
} else {
res.json(404, {status: 'invalid menu item'});
}
});
app.get('/*', function (req, res) {
res.json(404, {status: 'not found'});
});
http.createServer(app).listen(3000, function () {
console.log("Server ready at http://localhost:3000");
});