10.6. Creating a Basic REST API with NodeJS and ExpressJS: Implementing the HTTP Methods
Page 71 | Listen in audio
10.6. Creating a Basic REST API with NodeJS and ExpressJS: Implementing HTTP Methods
NodeJS is a software development platform that allows the execution of JavaScript on the server side. ExpressJS, on the other hand, is a framework for NodeJS that provides features for building web APIs in a quick and easy way. In this chapter, we'll explore how to create a basic REST API using NodeJS and ExpressJS, with a focus on implementing the HTTP methods.
Setting the environment
Before we start building our API, we need to install NodeJS and ExpressJS. You can download NodeJS from the official website, and after installation, you can install ExpressJS using the npm package manager that comes with NodeJS. The following command installs ExpressJS:
npm install express
Creating the basic structure of the project
For our project, we're going to create a folder called "api-rest-node", and inside that, we're going to create a file called "app.js". This file will be the entry point for our application. Inside "app.js", let's import the express and create an instance of it:
const express = require('express');
const app = express();
Next, let's tell our application to listen on port 3000:
app.listen(3000);
Implementation of HTTP methods
HTTP methods are the operations we can perform on a resource in a REST API. The most common are GET, POST, PUT and DELETE. Let's implement each of them.
GET
The GET method is used to retrieve data from a resource. In ExpressJS, we can implement a GET route as follows:
app.get('/resources', (req, res) => {
res.send('Retrieving resources');
});
Here we are telling ExpressJS to respond with "Retrieving resources" when a client makes a GET request for "/resources".
POST
The POST method is used to send data to a resource. Implementing a POST route is similar to implementing a GET route:
app.post('/resources', (req, res) => {
res.send('Creating a resource');
});
Here we are responding with "Creating a resource" when a client makes a POST request to "/resources".
PUT
The PUT method is used to update an existing resource. The implementation of a PUT route is as follows:
app.put('/resources/:id', (req, res) => {
res.send(`Updating the resource with id ${req.params.id}`);
});
Here we are responding with "Updating resource with id {id}" when a client makes a PUT request to "/resources/{id}".
DELETE
The DELETE method is used to delete a resource. The implementation of a DELETE route is as follows:
app.delete('/resources/:id', (req, res) => {
res.send(`Deleting resource with id ${req.params.id}`);
});
Here we are responding with "Deleting resource with id {id}" when a client makes a DELETE request to "/resources/{id}".
Conclusion
With this, we created a basic REST API with NodeJS and ExpressJS, implementing the most common HTTP methods. However, this is just the tip of the iceberg. There's so much more you can do with NodeJS and ExpressJS, including authentication, data validation, error handling, and more. We hope this guide has given you a good foundation to start exploring further.
Now answer the exercise about the content:
Which of the following HTTP methods is used to update an existing resource in a REST API implemented with NodeJS and ExpressJS?
You are right! Congratulations, now go to the next page
You missed! Try again.
Next page of the Free Ebook: