-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
49 lines (41 loc) · 1020 Bytes
/
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
var express= require('express');
var bp = require('body-parser');
var _ = require('underscore');
var app= express();
app.use(bp.json());
app.use(express.static('public'))
var tasks =[];
var taskId=1;
app.get('/getMyTasks', function (req, res) {
res.send(tasks);
})
app.post('/postMyTask', function (req, res) {
var data=req.body;
data.id=taskId++;
tasks.push(data);
res.json(data);
})
app.get('/getMyTask/:id', function (req, res) {
var todoId=parseInt(req.params.id,10);
var matchedTask=_.findWhere(tasks,{id:todoId});
if(matchedTask){
res.send(matchedTask);
}
else{
res.status(404).send();
}
})
app.delete('/deleteTask/:id', function (req, res) {
var todoId=parseInt(req.params.id,10);
var matchedTask=_.findWhere(tasks,{id:todoId});
if(!matchedTask){
res.status(404).json({"error":"id not found" ,"id":todoId});
}
else{
tasks=_.without(tasks,matchedTask);
res.send(matchedTask);
}
})
app.listen(3000,function(){
console.log('app is running on port 3000');
})