-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (63 loc) · 2.04 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
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var compression = require("compression");
var morgan = require("morgan");
var PORT = Number( process.env.PORT || 3000 );
var Counters = require("./lib/Counters");
app.use(morgan("combined"));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(compression());
function sendFile(name) {
return function(req, res) {
res.sendFile(__dirname + "/static/" + name);
};
}
app.get("/", sendFile("index.html"));
app.get("/bundle.js", sendFile("bundle.js"));
app.get("/app.css", sendFile("app.css"));
// [json] GET /api/v1/counters
// => [
// => {id: "asdf", title: "boop", count: 4},
// => {id: "zxcv", title: "steve", count: 3}
// => ]
app.get("/api/v1/counters", function(req, res) {
res.json(Counters.all())
});
// [json] POST {title: "bob"} /api/v1/counters
// => [
// => {id: "asdf", title: "boop", count: 4},
// => {id: "zxcv", title: "steve", count: 3},
// => {id: "qwer", title: "bob", count: 0}
// => ]
app.post("/api/v1/counter", function(req, res) {
res.json(Counters.create(req.body.title));
})
// [json] DELETE {id: "asdf"} /api/v1/counter
// => [
// => {id: "zxcv", title: "steve", count: 3},
// => {id: "qwer", title: "bob", count: 0}
// => ]
app.delete("/api/v1/counter", function(req, res) {
res.json(Counters.delete(req.body.id));
});
// [json] POST {id: "qwer"} /api/v1/counter/inc
// => [
// => {id: "zxcv", title: "steve", count: 3},
// => {id: "qwer", title: "bob", count: 1}
// => ]
app.post("/api/v1/counter/inc", function(req, res) {
res.json(Counters.inc(req.body.id));
});
// [json] POST {id: "zxcv"} /api/v1/counter/dec
// => [
// => {id: "zxcv", title: "steve", count: 2},
// => {id: "qwer", title: "bob", count: 1}
// => ]
app.post("/api/v1/counter/dec", function(req, res) {
res.json(Counters.dec(req.body.id));
});
app.get("*", sendFile("index.html"));
app.head("*", sendFile("index.html"));
app.listen(PORT, console.log.bind(null, "PORT: " + PORT));