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

ACT
1,452 views

Open Source Your Knowledge, Become a Contributor

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

Create Content

Content Negotiation

Express allows to do content-negotiation. Using response.format function, it can perform content-negotiation on the Accept HTTP header on the request object, when present.

It uses request.accepts() to select a handler for the request, based on the acceptable types ordered by their quality values. If the header is not specified, the first callback is invoked. When no match is found, the server responds with 406 “Not Acceptable”, or invokes the default callback.

(adapted from http://expressjs.com/en/api.html)

Content Negotiation Example
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
const express = require('express');
const port = 9000;
const app = express();
const postparser = require('./middleware/postparser');
const Todo = require('./models/todo');
app.use(postparser);
app.get('/', function(request, response){
Todo.all((err, todos) => response.format({
json: () =>{
response.status(200).json(todos);
},
html: () => {
response.status(406).send('Not supported yet\n');
}
}));
});
app.post('/', (request, response) => {
console.log(request.body);
var newTodo = JSON.parse(request.body);
Todo.add(newTodo);
response.status(201).json();
});
app.put('/:id', (request, response) => {
var id = request.params.id;
var updatedTodo = JSON.parse(request.body);
updatedTodo.id = parseInt(id);
Todo.update(updatedTodo, (err, data) => {
if(err)
{
response.status(404, 'The task is not found').send();
} else {
response.status(204).send(data);
}
});
});
app.delete('/:id', (request, response) => {
var id = parseInt(request.params.id);
Todo.delete(id, (err) => {
if(err){
response.status(404).send();
}else{
response.status(200).send();
}
});
});
app.listen(port);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Web Template - EJS

EJS is an embedded JavaScript template engine. Here are the some of the main features:

  • Control flow with <% %>
  • Escaped output with <%= %> (escape function configurable)
  • Unescaped raw output with <%- %>
  • Includes
  • Client-side support
  • Complies with the Express view system

For more information, you can visit: https://www.npmjs.com/package/ejs

To use it, it has to be installed:

npm install ejs
Exercise: Complete the Basic EJS Example
1
2
3
4
5
6
7
8
9
10
11
12
<html>
<head>
<title>Todos list</title>
</head>
<body>
<ul>
<% todos.forEach( (todo) => {%>
<li> ???TODO??? </li>
<% })%>
</ul>
</body>
</html>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Now, we have see the main parts on how to do a todo list with Node.js, let's wrap up!

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