-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
65 lines (62 loc) · 1.76 KB
/
server.js
File metadata and controls
65 lines (62 loc) · 1.76 KB
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
const express = require('express');
const app = express();
app.use(express.json());
app.listen(3000, () => {
console.log("starting listen on port 3000");
});
const tasks = new Array();
app.post('/tasks', function(req, res){
const { description } = req.body;
var newId = 0;
if(tasks.length > 0){
newId = tasks[tasks.length-1].id+1;
}
tasks.push({
id: newId,
description: description,
done: 'false',
});
res.json({message: "New task created."});
console.log(tasks);
});
app.get('/tasks', function(req, res){
res.json(tasks);
});
app.get('/tasks/:id', function(req, res){
var curr = tasks.filter(function(task){
if(task.id == req.params.id){
return true;
}
});
if(curr.length == 1){
res.json(curr[0])
} else {
res.status(404);//Set status to 404 as task was not found
res.json({message: "Not Found"});
}
});
app.put('/tasks/:id', function(req, res){
var updateIndex = tasks.map(function(task){
return task.id;
}).indexOf(parseInt(req.params.id));
if(updateIndex != -1){
const { description } = req.body;
tasks[updateIndex]['description'] = description;
res.json(tasks[updateIndex]);
} else {
res.status(404);//Set status to 404 as task was not found
res.json({message: "Not Found"});
}
});
app.delete('/tasks/:id', function(req, res){
var removeIndex = tasks.map(function(task){
return task.id;
}).indexOf(parseInt(req.params.id));
if(removeIndex != -1){
tasks.splice(removeIndex, 1);
res.json({message: "Deleted it"});
} else {
res.status(404);//Set status to 404 as task was not found
res.json({message: "Not Found"});
}
});