How to Create a Restful API Through Nodejs

From WikiHTP

In this new lesson, we are going to see how to create a RESTFUL API through NodeJs.

ROUTES[edit]

To define our endpoints, it is done through the express routing mechanism which we can initialize after configuring express.

const express = require('express'),
    app = express();

Having initialized this we can already defend our endpoints.

app.get('/', (req, res) =>
    res.send('Hello WOrld!')
);
  • The first parameter is the URL, in our case, we will tell it to respond to the main URL of our API.
  • The second parameter is a function that receives 2 objects. The first object refers to the request and the second to the response of the current request.

GET

When we want to list all the information of our user resource.

app.get('/users', (req, res) => {
    res.send('Users');
});

When we want to bring a single record of our user resource.

app.get('/users/:id', (req, res) => {
    res.send('User ' + req.params.id);
});

The parameters within the URI are made through ":", in our case we have defined a parameter: id and it is accessible through the request object and its params property.

req.params.id

POST

When we want to create a new resource, in our case of the user type.

app.post('/users', (req, res) => {
    // Created
    res.status(201);
    res.send(req.body);
});
  • We have told you that we want the response status code to be 201.
  • That it returns what the client has passed, which in practice should be the call to our insert from the database.

Note: it is usual that when we send information to the server we do it through a JSON. Therefore, we must enable this behaviour to our project because otherwise, we will not be able to read the information.

app.use(express.json());

PUT

When we want to update a resource.

app.put('/users/:id', (req, res) => {
    // Parameter reading
    let id = req.params.id;

    // No Content
    res.status(204);
    res.send(req.body);
});

DELETE

When we want to delete a resource.

app.delete('/users/:id', (req, res) => {
    // Parameter reading
    let id = req.params.id;
    console.log(id);

    // No Content
    res.status(204);
    res.send();
});

CONCLUSIONS[edit]

We have seen how to create an API in a simple way, what would be missing now is to organize ourselves because, like this, the project is not very scalable.

const express = require('express'),
    app = express(),
    port = 3000;

// Configiration
app.use(express.json());

// default endpoint
app.get('/', (req, res) => res.send('Hello World'));

app.get('/users', (req, res) => {
    res.send('Users');
});

app.get('/users/:id', (req, res) => {
    res.send('User ' + req.params.id);
});

app.post('/users', (req, res) => {
    // Created
    res.status(201);
    res.send(req.body);
});

app.put('/users/:id', (req, res) => {
    // Parameter reading
    let id = req.params.id;
    console.log(id);

    // No Content
    res.status(204);
    res.send(req.body);
});

app.delete('/users/:id', (req, res) => {
    // Parameter reading
    let id = req.params.id;
    console.log(id);

    // No Content
    res.status(204);
    res.send();
});

// on which port the application will run
app.listen(
    port,
    () => console.log(`Running on http://localhost:${port}`)
);

About This Tutorial

This page was last edited on 8 August 2020, at 23:41.