{"id":12174,"date":"2022-05-24T11:14:48","date_gmt":"2022-05-24T11:14:48","guid":{"rendered":"https:\/\/blog.cloudthat.com\/?p=12174"},"modified":"2024-06-25T10:59:59","modified_gmt":"2024-06-25T10:59:59","slug":"building-restful-api-using-nodejs","status":"publish","type":"blog","link":"https:\/\/www.cloudthat.com\/resources\/blog\/building-a-simple-restful-api-using-nodejs","title":{"rendered":"Building A Simple RESTful API Using NodeJS"},"content":{"rendered":"<table style=\"height: 231px;\" border=\"3\" width=\"349\">\n<tbody>\n<tr>\n<td>\n<h2><span style=\"color: #000080;\"><strong>TABLE OF CONTENT<\/strong><\/span><\/h2>\n<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#introduction\">1. Introduction<\/a><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#whatisrestapi\">2. What is REST API<\/a>?<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#needforrestapi\">3. Need for REST API<\/a><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#buildingrestapi \">4. Building REST API using NodeJS<\/a><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#bestpractices\">5. Best Practices for creating APIs<\/a><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#conclusion\">7. Conclusion<\/a><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#aboutcloudthat\">8. About CloudThat <\/a><\/td>\n<\/tr>\n<tr>\n<td><a href=\"#faqs\">9. Frequently Asked Questions (FAQs)<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"introduction\">1. Introduction<\/h2>\n<p>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.<\/p>\n<p>This blog will understand the REST API and create a simple REST API using NodeJS and ExpressJS.<\/p>\n<h2 id=\"whatisrestapi\">2. What is REST API?<\/h2>\n<p>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.<\/p>\n<p>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 \u2013<\/p>\n<ul>\n<li><strong>GET \u2013 Provides read access to the Resource<\/strong><\/li>\n<li><strong>POST \u2013 Create a new Resource<\/strong><\/li>\n<li><strong>PATCH \u2013 Updates an existing Resource<\/strong><\/li>\n<li><strong>DELETE \u2013 Removes a Resource<\/strong><\/li>\n<\/ul>\n<h2 id=\"needforrestapi\">3. Need for REST API<\/h2>\n<ol>\n<li>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.<\/li>\n<li>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.<\/li>\n<li>Because of the client and server separation, REST APIs are very scalable.<\/li>\n<\/ol>\n<h2 id=\"buildingrestapi\">4. Building REST API using NodeJS<\/h2>\n<p>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.<\/p>\n<p>In this application, I will be using VS Code Editor. You can choose your IDE or Code Editor.<\/p>\n<p>Step 1: Create your project directory, open the terminal, and navigate to the project directory. In your terminal, give the command <strong>npm init<\/strong> <strong>-y. <\/strong>It will generate a boilerplate for our node application.<\/p>\n<p>Step 2: Next, we will install Express.JS using the command <strong>npm i express<\/strong> in our application.<\/p>\n<p>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 <strong>npm i -g nodemon.<\/strong><\/p>\n<p><strong>So, our package.json looks like this-<\/strong><\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/resta.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12179\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/resta.png\" alt=\"Rest API\" width=\"825\" height=\"562\" \/><\/a><\/p>\n<p>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 \u2018app.js\u2019.<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/restb.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12180\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/restb.png\" alt=\"Rest API\" width=\"828\" height=\"322\" \/><\/a><\/p>\n<p>Step 5: First we will create our \u201c\/\u201d route which will run as soon our application starts.<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/restc.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12182\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/restc.png\" alt=\"Rest API\" width=\"735\" height=\"180\" \/><\/a><\/p>\n<p>As we are using an object as our database, so we will create the object of a book<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/restd.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12183\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/restd.png\" alt=\"Rest API\" width=\"589\" height=\"181\" \/><\/a><\/p>\n<p>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 \u201cGET\u201d method.<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/reste.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12184\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/reste.png\" alt=\"Rest API\" width=\"592\" height=\"147\" \/><\/a><\/p>\n<p>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.<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/restf.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12185\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/restf.png\" alt=\"Rest API\" width=\"903\" height=\"226\" \/><\/a><\/p>\n<p>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 \u201cPOST\u201d operation.<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/restg.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12186\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/restg.png\" alt=\"Rest API\" width=\"712\" height=\"339\" \/><\/a><\/p>\n<p>Step 8: Now we will perform the UPDATE operation. We will use the \u201cPATCH\u201d 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.<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/resth.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12187\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/resth.png\" alt=\"Rest API\" width=\"903\" height=\"226\" \/><\/a><\/p>\n<p>Step 9: At last, we will implement the DELETE operation. Here we will use the \u201cDELETE\u201d method.<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/resti.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12188\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/resti.png\" alt=\"Rest API\" width=\"903\" height=\"275\" \/><\/a><\/p>\n<p>Now we have created our CRUD operation. To test whether all our API handlers are working properly or not we will use POSTMAN.<\/p>\n<p>So first we will test our \u201cREAD\u201d operation. We will provide the URL as \u201c<a href=\"http:\/\/localhost:3000\/api\/books\">localhost:3000\/api\/books<\/a>\u201d and set the method as \u201cGET\u201d<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/rest1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12201\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/rest1.png\" alt=\"REST API\" width=\"909\" height=\"401\" \/><\/a><\/p>\n<p>Now we will find by ID so we will provide a URL with \/id<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/rest2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12202\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/rest2.png\" alt=\"REST API\" width=\"909\" height=\"354\" \/><\/a><\/p>\n<p>Now we will test our POST operation. For that, we will set our method to POST and in Body<\/p>\n<p>We will be sending the data as JSON<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/rest3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12203\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/rest3.png\" alt=\"REST API\" width=\"909\" height=\"439\" \/><\/a><\/p>\n<p>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.<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/rest4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12204\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/rest4.png\" alt=\"REST API\" width=\"907\" height=\"435\" \/><\/a><\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/rest5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12205\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/rest5.png\" alt=\"REST API\" width=\"909\" height=\"483\" \/><\/a><\/p>\n<p>Finally, we will be deleting our data. For that, we will be using the DELETE method.<\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/rest6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12206\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/rest6.png\" alt=\"REST API\" width=\"909\" height=\"313\" \/><\/a><\/p>\n<p><a href=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/rest7.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12207\" src=\"https:\/\/d1f7lmxeo98xps.cloudfront.net\/resources\/wp-content\/uploads\/2022\/11\/rest7.png\" alt=\"REST API\" width=\"909\" height=\"469\" \/><\/a><\/p>\n<p>In this way, we can create a REST API using NodeJS.<\/p>\n<h3 id=\"bestpractices\">5. Best Practices for creating APIs<\/h3>\n<ol>\n<li>There is a common practice that APIs should accept JSON requests as their payload and respond back in the same JSON format.<\/li>\n<li>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.<\/li>\n<li>Most of the communications between the client and the server are private. Therefore, we must use SSL\/TSL for security purposes.<\/li>\n<\/ol>\n<h3 id=\"conclusion\">6. Conclusion<\/h3>\n<p>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.<\/p>\n<h3 id=\"aboutcloudthat\">7. About CloudThat<\/h3>\n<p><a href=\"https:\/\/www.cloudthat.com\/\"><strong>CloudThat\u00a0<\/strong><\/a>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\u00a0a robust\u00a0cloud computing ecosystem by disseminating\u00a0knowledge on technological intricacies within the cloud space.\u00a0Our blogs, webinars,\u00a0case studies, and white papers\u00a0enable all the stakeholders in the cloud computing sphere.<\/p>\n<p>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\u00a0our<strong>\u00a0<\/strong><a href=\"https:\/\/www.cloudthat.com\/expert-advisory\/?utm_source=blog-website&amp;utm-medium=text-link&amp;utm_campaign=expert-advisory\"><strong>Expert Advisory<\/strong>\u00a0<\/a>page\u00a0and\u00a0<a href=\"https:\/\/www.cloudthat.com\/managed-services-packages\/\"><strong>Managed Services Package<\/strong><\/a>\u00a0that is<strong>\u00a0<a href=\"https:\/\/cloudthat.com\/?utm_source=blog-website&amp;utm-medium=text-link&amp;utm_campaign=cloudthat.com\/\">CloudThat<\/a><\/strong>\u2019s\u00a0offerings.<\/p>\n<h3 id=\"faqs\">8. Frequently Asked Questions (FAQs)<\/h3>\n<ol>\n<li>\n<h4><strong>Why is REST API stateless?<\/strong><\/h4>\n<\/li>\n<\/ol>\n<p>Ans -&gt; 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.<\/p>\n<ol start=\"2\">\n<li>\n<h4><strong>What is the difference between PUT and PATCH methods?<\/strong><\/h4>\n<\/li>\n<\/ol>\n<p>Ans -&gt; 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.<\/p>\n","protected":false},"author":286,"featured_media":12424,"parent":0,"comment_status":"open","ping_status":"open","template":"","blog_category":[3606,3608,3607],"user_email":"satyamd@cloudthat.com","published_by":"324","primary-authors":"","secondary-authors":"","acf":[],"_links":{"self":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/12174"}],"collection":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog"}],"about":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/types\/blog"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/users\/286"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/comments?post=12174"}],"version-history":[{"count":1,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/12174\/revisions"}],"predecessor-version":[{"id":45629,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/12174\/revisions\/45629"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/media?parent=12174"}],"wp:term":[{"taxonomy":"blog_category","embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog_category?post=12174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}