Back
Close

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

ACT
7,394 views

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
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
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.

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants