Building A Simple RESTful API Using NodeJS

May 24, 2022 | Comments(0) |

TABLE OF CONTENT

1. Introduction
2. What is REST API?
3. Need for REST API
4. Building REST API using NodeJS
5. Best Practices for creating APIs
7. Conclusion
8. About CloudThat
9. Frequently Asked Questions (FAQs)

1. Introduction

There are many popular frameworks for making backend applications, and one of the most popular ones is NodeJS. NodeJS is an open-source server-based environment and runs on various platforms like Windows, Linux, Unix, Mac OS, etc., and uses JavaScript as its programming language. In addition, NodeJS uses asynchronous mode programming for its input-output operations that prevent the blocking of JavaScript code.

This blog will understand the REST API and create a simple REST API using NodeJS and ExpressJS.

2. What is REST API?

REST stands for Representational State Transfer. It is an architectural style that is used to create web services. It uses HTTP protocol and makes HTTP requests over World Wide Web. RESTful web services have a Uniform Resource Identifier (URI) like text, JSON, XML, etc., but JSON is the most popular.

REST APIs are stateless operations, i.e., the current operation does not depend on previous operations. The main functionalities which can be created through REST architecture are Create, Read, Update and Delete (CRUD). The commonly used HTTP methods for these operations are –

  • GET – Provides read access to the Resource
  • POST – Create a new Resource
  • PATCH – Updates an existing Resource
  • DELETE – Removes a Resource

3. Need for REST API

  1. REST API is independent of the programming language and platform. It can be made with any programming language and can have a variety of syntaxes. Furthermore, it can be built on any platform like Windows, Linux, MAC OS, etc.
  2. Because of the REST API, the client and the server can be used independently and can be used in different development projects. Furthermore, because of REST API, the client and the server are loosely coupled and can be evolved separately.
  3. Because of the client and server separation, REST APIs are very scalable.

4. Building REST API using NodeJS

Here, we will be creating a simple CRUD REST API using NodeJS and ExpressJS. First, we need to install NodeJS and ExpressJS in our application. We will have an object that contains our data and does not use any database for database purposes.

In this application, I will be using VS Code Editor. You can choose your IDE or Code Editor.

Step 1: Create your project directory, open the terminal, and navigate to the project directory. In your terminal, give the command npm init -y. It will generate a boilerplate for our node application.

Step 2: Next, we will install Express.JS using the command npm i express in our application.

Step 3: Finally, we will install nodemon, which restarts the NodeJS server every time there is a change in our code. nodemon detects the changes and restarts the server for you. In terminal write npm i -g nodemon.

So, our package.json looks like this-

Rest API

Step 4: Now, we will set up a basic expressJS server that will run our application on port 3000. So, we will create a file ‘app.js’.

Rest API

Step 5: First we will create our “/” route which will run as soon our application starts.

Rest API

As we are using an object as our database, so we will create the object of a book

Rest API

Step 6: Now, we will perform a READ operation. This will get all the books present in our object. For a read operation, we will be using the “GET” method.

Rest API

If we want to read our data of any particular id, we will pass id in our route and will check if that id is present in our object or not.

Rest API

Step 7: Now we will perform CREATE operation. This operation will create an entry in our object. To create an operation we will use the “POST” operation.

Rest API

Step 8: Now we will perform the UPDATE operation. We will use the “PATCH” method since it will enable us to send only the fields we want to change. So, its route will be /books/:id and will send the fields we want to change.

Rest API

Step 9: At last, we will implement the DELETE operation. Here we will use the “DELETE” method.

Rest API

Now we have created our CRUD operation. To test whether all our API handlers are working properly or not we will use POSTMAN.

So first we will test our “READ” operation. We will provide the URL as “localhost:3000/api/books” and set the method as “GET”

REST API

Now we will find by ID so we will provide a URL with /id

REST API

Now we will test our POST operation. For that, we will set our method to POST and in Body

We will be sending the data as JSON

REST API

Now, we will test our UPDATE operation. For that, we will set our method to PATCH and along with the URL, we will send the id of the data we want to update and update the data in the body.

REST API

REST API

Finally, we will be deleting our data. For that, we will be using the DELETE method.

REST API

REST API

In this way, we can create a REST API using NodeJS.

5. Best Practices for creating APIs

  1. There is a common practice that APIs should accept JSON requests as their payload and respond back in the same JSON format.
  2. There is a best practice to always use status codes in their REST APIs design. With the status codes, developers can easily identify the issue which helps in saving time.
  3. Most of the communications between the client and the server are private. Therefore, we must use SSL/TSL for security purposes.

6. Conclusion

So, we have completed our article on how to build a simple REST API using NodeJS. Feel free to ask any queries you may have while working on the same, and I will be happy to help you solve them.

7. About CloudThat

CloudThat is also the official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner and Microsoft gold partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.

Drop a query if you have any questions regarding REST APIs, NodeJS, or ExpressJS, and I will get back to you quickly. To get started, go through our Expert Advisory page and Managed Services Package that is CloudThat’s offerings.

8. Frequently Asked Questions (FAQs)

  1. Why is REST API stateless?

Ans -> The state of the client is not maintained on the server. This is statelessness. The context is provided by the client to the server by which the requests get processed by the server.

  1. What is the difference between PUT and PATCH methods?

Ans -> PUT is an HTTPS method where the client sends the data which modifies the entire Resource. PATCH is a method of modifying resources where the client sends the partial data that needs to be updated without modifying the entire data.


Leave a Reply