Building a Basic Todo List REST API in Node.js with Express

ACT
3,027 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Parameters

Express allows to extract parameters from the url. A parameter is defined in an URL with the ":" prefix. It is aviailable as a member of request.params.

var myParam = request.params.myParam

Route parameters are named URL segments that are used to capture the values specified at their position in the URL. The captured values are populated in the req.params object, with the name of the route parameter specified in the path as their respective keys.

from http://expressjs.com/en/guide/routing.html

Exercise: Complete the PUT function
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
const express = require('express');
const port = 9000;
const app = express();
const bodyParser = require('body-parser');
var todos = [{id:1, title:'buy the milk'}, {id:2, title:'rent a car'}, {id:3, title:'feed the cat'}];
app.use(bodyParser.json())
app.get('/', (request, response) => response.status(200).json(todos));
app.post('/', (request, response) => {
var newTodo = JSON.parse(request.body);
newTodo.id = todos.length + 1;
todos.push(newTodo);
response.status(201).json();
});
app.put('/:id', (request, response) => {
var id = ???TODO??? // retrieve the id parameter value
if (todos[id - 1]){
todos[id - 1] = request.body;
response.status(204).send();
}else{
response.status(404, 'The task is not found').send();
}
});
app.listen(port);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Last Action: DELETE

To conclude this part of the tutorial, please find bellow a basic implementation of the DELETE method.

DELETE Method
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
const express = require('express');
const port = 9000;
const app = express();
const bodyParser = require('./bodyparser');
var todos = [{id:1, title:'buy the milk'}, {id:2, title:'rent a car'}, {id:3, title:'feed the cat'}];
var count = todos.length;
app.use(bodyParser);
app.get('/', (request, response) => response.status(200).json(todos));
app.post('/', (request, response) => {
var newTodo = JSON.parse(request.body);
count = count +1;
newTodo.id = count;
todos.push(newTodo);
response.status(201).json();
});
app.put('/:id', (request, response) => {
var id = request.params.id;
if (todos[id]){
var updatedTodo = JSON.parse(request.body);
todos[id] = updatedTodo;
response.status(204).send();
}else{
response.status(404, 'The task is not found').send();
}
});
app.delete('/:id', (request, response) => {
var id = parseInt(request.params.id);
if(todos.filter(todo => todo.id == id).length !== 0){
todos = todos.filter(todo => todo.id !== id);
response.status(200).send();
}else{
response.status(404).send();
}
});
app.listen(port);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

As you can see, all the code is in one file, we do not have a clear separation of each layer. Let's move on how the next page to add a database and also refactor and organize our code with modules.

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content