-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
124 lines (103 loc) · 3.61 KB
/
main.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'use babel'; // use babel transpiler
require('./util/db-connect'); // db connection
var express = require('express');
var app = express();
const path = require('path');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
var Todo = mongoose.model('Todo');
var util = require('./util/util');
app.use(bodyParser.json()); // REQUEST PARSER
app.use('/res', express.static('static')); // NOTE access via /res/js/app.js
// ROUTING
app
.route('/')
.get((req, res) => {
res.status(200).sendFile('index.html', { root: path.join('.', 'static') }); // root file
});
app
.route('/todos/')
.get((req, res) => {
Todo.find({})
.sort( '-updated_at' )
.exec( ( err, todos ) => {
res.status(200).send({tasks: todos});
});
})
.post((req, res) => {
// curl -X POST -H "Content-Type: application/json" -d '{"task":"yes","status":true}' -w "\n" http://localhost:3000/
var _task = JSON.parse(JSON.stringify(req.body));
_task.task_id = util.makeid();
Todo.find({})
.exec((err, todos) => {
if (todos.length >= 30){
return res.status(403).send({result:'failed', message:'reached limit of 30 tasks.'});
}
new Todo({
task_id: _task.task_id,
task: _task.task,
status: false
}).save(() => {
res.status(201).send({task_id: _task.task_id});
});
});
});
// app.route('/:task_id') // console.log('delete a new task', req.params.task_id);
app
.route(/^\/todos\/(\w+)/)
.get((req, res) => {
var target_task_id = req.params[0];
Todo.find({task_id: target_task_id})
.sort( '-updated_at' )
.exec( ( err, todos ) => {
if (todos.length){
return res.status(200).send(todos[0]);
}
res.status(404).send('NOT FOUND');
});
})
.put((req, res) => {
// curl -X PUT -H "Content-Type: application/json" -d '{"task":"NewTask","status":true}' -w "\n" http://localhost:3000/t_id
var target_task_id = req.params[0];
var taskExist = false;
Todo.find({})
.exec(( err, todos ) => {
for (var task of todos) {
if (task.task_id === target_task_id){ // TODO use mongo filter
taskExist = true;
res.status(200).send({result: "ok"});
task.task = req.body.task;
task.status = req.body.status;
task.save((err) => {
if( err ) console.log('error db update');
});
}
}
if (!taskExist) { res.status(404).send('NOT FOUND'); }
});
})
.delete((req, res) => {
// curl -X DELETE -H "Content-Type: application/json" -w "\n" http://localhost:3000/t_id
var target_task_id = req.params[0];
Todo.find({})
.exec(( err, todos ) => {
for (var task of todos) {
if (task.task_id === target_task_id){ // TODO use mongo filter
var taskExist = true;
res.status(200).send({result: "ok"});
task.remove((err) => {
if( err ) console.log('error db update');
});
}
}
if (!taskExist) { res.status(404).send('NOT FOUND'); }
});
});
app.use((req, res) => { // default request handle (should put in bottom)
return res.status(404).send('NOT FOUND');
});
// MAIN
var port = process.env.PORT || 3000;
app.listen(port, () => {
console.log('Example app listening on port', port);
});