- Consulting
- Training
- Partners
- About Us
x
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.
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 –
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-
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’.
Step 5: First we will create our “/” route which will run as soon our application starts.
As we are using an object as our database, so we will create the object of a book
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.
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.
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.
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.
Step 9: At last, we will implement the DELETE operation. Here we will use the “DELETE” method.
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”
Now we will find by ID so we will provide a URL with /id
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
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.
Finally, we will be deleting our data. For that, we will be using the DELETE method.
In this way, we can create a REST API using NodeJS.
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.
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.
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.
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.
Voiced by Amazon Polly |
Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!
Click to Comment