-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
82 lines (66 loc) · 2.65 KB
/
routes.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
77
78
79
80
81
82
module.exports = function(app, tools) {
// api ---------------------------------------------------------------------
// get all todos
app.get('/api/todos', function(req, res) {
//Get the todos from json file.
//Alternativelly, it could be a database
global.fs.readFile(global.todosFile, 'utf8', function(err, todos) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
res.send(err)
todos = JSON.parse(todos);
res.setHeader('Cache-Control', 'no-cache');
res.json(todos); // return all todos in JSON format
});
});
// // create todo and send back all todos after creation
app.post('/api/todos', function(req, res) {
//Get the todos from json file.
//Alternativelly, it could be a database
global.fs.readFile(global.todosFile, 'utf8', function(err, todos) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
res.send(err)
var todos = JSON.parse(todos);
var newTodo = {
id: global.uuid.v1(),
author: req.body.author,
text: req.body.text,
};
todos.push(newTodo);
global.fs.writeFile(global.todosFile, JSON.stringify(todos, null, 4), function (err) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
res.send(err)
res.setHeader('Cache-Control', 'no-cache');
res.json(todos);
global.io.emit('todos', todos);
});
});
});
//
// // delete a todo
app.delete('/api/todos/:todo_id', function(req, res) {
var todo_id = req.params.todo_id;
global.fs.readFile(global.todosFile, 'utf8', function(err, todos) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
res.send(err)
todos = tools.deleteItemInArrayById(todo_id, todos);
global.fs.writeFile(global.todosFile, JSON.stringify(todos, null, 4), function (err) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
res.send(err)
//Once the files is updated send the TODOs to alll the clients via the WebSocket
res.setHeader('Cache-Control', 'no-cache');
res.json('success');
global.io.emit('todos', todos);
});
});
});
// application -------------------------------------------------------------
app.get('*', function(req, res) {
tools.logToConsole('NODEJS_SERVER', 'Remote client ' + req.connection.remoteAddress +':'+ req.connection.remotePort + ' get the TODO app');
res.sendFile('index.html', {root: __dirname + '/public'}); // load the single view file (react will handle the page changes on the front-end)
});
};