-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
141 lines (128 loc) · 3.8 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var express = require('express');
var MongoClient = require('mongodb').MongoClient;
var q = require('q');
var bodyParser = require('body-parser');
var url = "mongodb://localhost:27017";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
console.log("数据库连接成功");
db.close();
});
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static("./static"));
// 查找所有todo list
app.get('/todo/list', function(req,res,next){
MongoClient.connect(url, function(err, db) {
var dblocal = db.db("local");
dblocal.collection("todo").find({}).toArray(function(err, result) {
if(err){
res.status(500).json({
message: err,
code: 500,
success: false
})
}else{
var count = 0;
// 计算待办事件总数
result.forEach(e => {
if(!e.completed){
count++;
}
});
res.status(200).json({
data: {
list: result,
count: count
},
code: 200,
success: true
})
}
db.close();
});
});
});
// 新增todo
app.post('/todo/add', function(req,res,next){
var temp = req.body;
temp.completed = temp.completed==='false' ? false : true;
temp.id = new Date().getTime();
MongoClient.connect(url, function(err, db) {
var dblocal = db.db("local");
dblocal.collection("todo").insertOne(temp, function(err, result) {
if(err){
res.status(500).json({
message: err,
code: 500,
success: false
})
}else{
res.status(200).json({
data: result,
code: 200,
success: true
})
}
db.close();
});
});
});
// 更新todo状态
app.post('/todo/update', function(req,res,next){
var temp = req.body;
temp.completed = temp.completed==='false' ? false : true;
temp.id = parseInt(temp.id);
MongoClient.connect(url, function(err, db) {
var dblocal = db.db("local");
dblocal.collection("todo").updateOne({
"id" : temp.id
}, {
$set: { "completed": temp.completed },
}, function(err, result) {
if(err){
res.status(500).json({
message: err,
code: 500,
success: false
})
}else{
res.status(200).json({
data: result,
code: 200,
success: true
})
}
db.close();
});
});
});
// 删除todo
app.delete('/todo/delete', function(req,res,next){
var temp = req.body;
temp.id = parseInt(temp.id);
MongoClient.connect(url, function(err, db) {
var dblocal = db.db("local");
dblocal.collection("todo").deleteOne({
"id" : temp.id
}, function(err, result) {
if(err){
res.status(500).json({
message: err,
code: 500,
success: false
})
}else{
res.status(200).json({
data: result,
code: 200,
success: true
})
}
db.close();
});
});
})
var server = app.listen(3000, function() {
console.log('Listening on port %d', server.address().port);
});