forked from Kuuchin/nodejs_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
62 lines (52 loc) · 1.42 KB
/
server.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
/**
* Created for DevOps course on 21/11/2016.
*/
const express = require('express');
var jade = require('jade');
var todo_utils = require('./todo_utils');
var bodyParser = require('body-parser');
var jsonParser = bodyParser.json();
const app = express();
const port = 8080;
app.use(express.static(__dirname + '/public'));
app.use(require('body-parser').urlencoded({ extended: true }));
app.set('views', __dirname + '/views');app.set('view engine', 'jade');
todo_utils._init();
todo_utils._new();
app.get('/', (req, res) => {
todo_utils._getAll((todoList) => {
res.render('index',
{ todoList : todoList.join(' - ') }
);
});
});
app.get('/detail/:_id', (req, res) => {
todo_utils._get(req.params._id, (things) => {
res.render('detail',
{ things : things }
);
});
});
app.get('/new', (req, res) => {
todo_utils._new();
res.redirect('/');
});
app.post('/add', jsonParser, (req, res) => {
todo_utils._add(req.body.things, (todoList) => {
res.redirect('/');
});
});
app.post('/delete', jsonParser, (req, res) => {
todo_utils._del(req.body._id, (things) => {
res.redirect('/');
});
});
app.post('/get', jsonParser, (req, res) => {
res.redirect('/detail/' + req.body._id);
});
app.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
});