-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
76 lines (64 loc) · 1.35 KB
/
app.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
require("colors"); // Add colors to console
var path = require("path");
// Webpack requires
var wp = require("webpack");
var wpMw = require("webpack-dev-middleware");
var wpConfig = require("./webpack.config");
// Express
var express = require("express");
var app = express();
// Body parser
var bodyParser = require("body-parser");
app.use(bodyParser.json());
// Public folder
app.use("/client", express.static("public"));
//REST API
// simple in memory database
var database = {
"0" : {
"id" : "0",
"text" : "Todo 0",
"status" : 0,
},
"1" : {
"id" : "1",
"text" : "Todo 1",
"status" : 0,
},
"2" : {
"id" : "2",
"text" : "Todo 2",
"status" : 1,
},
};
var nextTodoId = 3
// Get a new todo id
app.get("/todo", (req, res) => {
res.json({ "id" : "" + nextTodoId++ });
});
// Get all todos
app.get("/todos", (req, res) => {
res.json(database);
});
// Save all todos
app.put("/todos", (req, res) => {
database = req.body;
res.status(200);
res.end();
});
// Webpack setup
app.use(wpMw(wp(wpConfig), {
"hot" : true,
"stats" : {
"colors" : true,
"chunks" : false,
},
}));
// Default route
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "/client/index.html"));
})
// Start the server
app.listen(3000, () => {
console.log("SERVER READY ON [".green + "localhost:3000".cyan + "]".green);
});