-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·77 lines (54 loc) · 1.51 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var express = require('express'),
http = require('http'),
beers = require('./data/beers.json');
var app = express()
.use(express.bodyParser())
.use(express.static('public'));
var getNewUniqueID = function (dataSource) {
var id = (Math.random() + ' ').substring(2,10) + (Math.random() + ' ').substring(2,10);
// for the one in a bazillion chance that the ID already exists
if (dataSource[id] != undefined) {
getNewUniqueID();
return;
} else {
return id;
}
};
// view a list of all beers
app.get('/beers', function (req, res) {
res.json(beers);
});
// get a specific beer
app.get('/beers/:beer', function (req, res) {
if (typeof beers[req.params.beer] === 'undefined') {
res.json(404, {status: 'not found - invalid beer code'});
} else {
res.json(beers[req.params.beer]);
}
});
// delete a beer
app.delete('/beers/:beer', function (req, res) {
var index = req.params.beer;
if (typeof beers[index] === 'undefined') {
res.json(404, {status: 'not found - invalid beer code'});
} else {
delete beers[index];
res.json({status: 'deleted ' + index});
}
});
// add or update a beer
app.post('/beers', function (req, res) {
var code = req.body.code;
if (!code) {
code = getNewUniqueID(beers);
req.body.code = code;
}
beers[code] = req.body;
res.json(req.body);
});
app.get('/*', function (req, res) {
res.json(404, {status: 'not found'});
});
http.createServer(app).listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function () {
console.log("Server rockin.");
});