-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
52 lines (44 loc) · 1001 Bytes
/
Copy pathserver.js
File metadata and controls
52 lines (44 loc) · 1001 Bytes
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
/**
* Created by Medo on 5/15/2016.
*/
var express = require('express');
var app = express();
var port = process.env.PORT || 6336;
var todos = [
{
id: 1,
description: 'eat',
completed: false
},
{
id: 2,
description: 'drink water',
completed: true
},
{
id: 3,
description: 'sleep',
completed: false
}
];
app.get('/', function (request, response) {
response.send('todo-api root page');
});
app.get('/todos', function (request, response) {
response.json(todos);
});
app.get('/todos/:id', function (request, response) {
var todoId = parseInt(request.params.id, 10);
var matchedTodo;
todos.forEach(function (data) {
if(data.id === todoId)
matchedTodo = data;
});
if(matchedTodo)
response.json(matchedTodo);
else
response.status(404).send();
});
app.listen(port, function () {
console.log('todo-api running on port ' + port);
});