

{"id":104995,"date":"2021-12-18T09:00:21","date_gmt":"2021-12-18T03:30:21","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=104995"},"modified":"2021-12-18T12:52:20","modified_gmt":"2021-12-18T07:22:20","slug":"nodejs-rest-api","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/nodejs-rest-api\/","title":{"rendered":"Node.js Rest API"},"content":{"rendered":"<p>In this blog, you will get to know what nodejs rest api is, how we can create a restful api. So let&#8217;s start!!!<\/p>\n<h3>Nodejs Rest Architecture:<\/h3>\n<p>It is a web standards-based architecture that uses HTTP Protocol. Every component is a resource and these resources are accessed by an interface using HTTP methods.<\/p>\n<h3>Node js RESTful Web services:<\/h3>\n<p>A web service is used for exchanging data between applications. Web services which are based on REST architecture are called restful web services. It uses http methods to implement rest architecture.<\/p>\n<h3>HTTP Methods:<\/h3>\n<ul>\n<li>GET \u2212 It is used to read information from a resource.<\/li>\n<li>PUT \u2212 It is used to update a new resource.<\/li>\n<li>DELETE \u2212 It is used to remove a resource.<\/li>\n<li>POST \u2212 It is used to create a new resource<\/li>\n<\/ul>\n<h3>Creating a restful student Details:<\/h3>\n<p>For demonstrating the restful api we will be creating a dummy database of students and it will be stored in a json file. It will look like below.<\/p>\n<p>Json file looks as below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">{\r\n    \"student1\": {\r\n        \"name\": \"abc\",\r\n        \"id\": 1\r\n    },\r\n    \"student2\": {\r\n        \"name\": \"def\",\r\n        \"id\": 2\r\n    }\r\n}\r\n<\/pre>\n<h3>Nodejs GET Method for Listing students:<\/h3>\n<p>In order to view all the students we will be using the get method. The url for getting the list of all the students is localhost:3000\/students.<\/p>\n<p><strong>Code for getting all the students:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var express = require('express');\r\nvar app = express();\r\nvar fs = require(\"fs\");\r\napp.get('\/students', function (req, res) {\r\n    fs.readFile(__dirname + \"\/\" + \"student.json\", 'utf8', function (err, data) {\r\n        console.log(data);\r\n        res.end(data);\r\n    });\r\n})\r\napp.listen(3000, function () {\r\n    console.log(\"server running on port 3000\")\r\n})\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-get-result.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-105024\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-get-result.webp\" alt=\"nodejs get result\" width=\"1366\" height=\"726\" \/><\/a><\/p>\n<h3>Nodejs POST METHOD for adding student<\/h3>\n<p>In order to add students we will be using the post method. The url for getting the list of all the students is localhost:3000\/addstudent. Now send a post request and then check the student.json file you will see another student has been added.<\/p>\n<p><strong>Code for adding a student:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var express = require('express');\r\nvar app = express();\r\nvar fs = require(\"fs\");\r\napp.get('\/students', function (req, res) {\r\n    fs.readFile(__dirname + \"\/\" + \"student.json\", 'utf8', function (err, data) {\r\n        console.log(data);\r\n        res.end(data);\r\n    });\r\n})\r\nlet student = {\r\n    \"student3\": {\r\n        \"name\": \"zyx\",\r\n        \"id\": 3\r\n    }\r\n}\r\n \r\napp.post('\/addstudent', function (req, res) {\r\n    fs.readFile(__dirname + \"\/\" + \"student.json\", 'utf8', function (err, data) {\r\n        data = JSON.parse(data);\r\n        data[\"student3\"] = user[\"student3\"];\r\n        console.log(data);\r\n        res.end(JSON.stringify(data));\r\n    });\r\n})\r\napp.listen(3000, function () {\r\n    console.log(\"server running on port 3000\")\r\n})\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-post-result.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-105025\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-post-result.webp\" alt=\"nodejs post result\" width=\"1366\" height=\"726\" \/><\/a><\/p>\n<h3>Nodejs Delete Method for deleting a student;<\/h3>\n<p>In order to delete a student from the json file we will be using the delete method. The below code deletes a student with id 2.<\/p>\n<p>For deleting a student, send a delete request to localhost:3000\/deletestudent.<\/p>\n<p>Now check the json file and you will see the student with id 2 is no longer present in our json file.<\/p>\n<p><strong>Code for deleting a student:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var express = require('express');\r\nvar app = express();\r\nvar fs = require(\"fs\");\r\napp.get('\/students', function (req, res) {\r\n    fs.readFile(__dirname + \"\/\" + \"student.json\", 'utf8', function (err, data) {\r\n        console.log(data);\r\n        res.end(data);\r\n    });\r\n}\r\nlet student = {\r\n    \"student3\": {\r\n        \"name\": \"zyx\",\r\n        \"id\": 3\r\n    }\r\n}\r\napp.post('\/addstudent', function (req, res) {\r\n    fs.readFile(__dirname + \"\/\" + \"student.json\", 'utf8', function (err, data) {\r\n        data = JSON.parse(data);\r\n        data[\"student3\"] = user[\"student3\"];\r\n        console.log(data);\r\n        res.end(JSON.stringify(data));\r\n    });\r\n})\r\nconst id = 2;\r\n \r\napp.delete('\/deletestudent', function (req, res) {\r\n    fs.readFile(__dirname + \"\/\" + \"student.json\", 'utf8', function (err, data) {\r\n        data = JSON.parse(data);\r\n        delete data[\"student\" + 2];\r\n        console.log(data);\r\n        res.end(JSON.stringify(data));\r\n    });\r\n})\r\n \r\napp.listen(3000, function () {\r\n    console.log(\"server running on port 3000\")\r\n})\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-delete-result.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-105026\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-delete-result.webp\" alt=\"nodejs delete result\" width=\"1366\" height=\"726\" \/><\/a><\/p>\n<h3>Conclusion:<\/h3>\n<p>In this article, we have gone through a detailed overview of restful api and its methods with codes and examples, we hope you were able to learn.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, you will get to know what nodejs rest api is, how we can create a restful api. So let&#8217;s start!!! Nodejs Rest Architecture: It is a web standards-based architecture that uses&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":105023,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25259],"tags":[25986,25987,25989,25988],"class_list":["post-104995","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js-tutorials","tag-node-js-rest-api","tag-nodejs-create-method","tag-nodejs-delete-method","tag-nodejs-post-method"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Node.js Rest API - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn about nodejs Rest API. See REST Architecture, Web Services, HTTPS Methods, Get Method, Post Method, Delete Method etc.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/nodejs-rest-api\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Node.js Rest API - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn about nodejs Rest API. See REST Architecture, Web Services, HTTPS Methods, Get Method, Post Method, Delete Method etc.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/nodejs-rest-api\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-12-18T03:30:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-18T07:22:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-rest-api.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Node.js Rest API - DataFlair","description":"Learn about nodejs Rest API. See REST Architecture, Web Services, HTTPS Methods, Get Method, Post Method, Delete Method etc.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/nodejs-rest-api\/","og_locale":"en_US","og_type":"article","og_title":"Node.js Rest API - DataFlair","og_description":"Learn about nodejs Rest API. See REST Architecture, Web Services, HTTPS Methods, Get Method, Post Method, Delete Method etc.","og_url":"https:\/\/data-flair.training\/blogs\/nodejs-rest-api\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2021-12-18T03:30:21+00:00","article_modified_time":"2021-12-18T07:22:20+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-rest-api.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/nodejs-rest-api\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-rest-api\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Node.js Rest API","datePublished":"2021-12-18T03:30:21+00:00","dateModified":"2021-12-18T07:22:20+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-rest-api\/"},"wordCount":364,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-rest-api\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-rest-api.webp","keywords":["Node.js Rest API","Nodejs Create Method","Nodejs Delete Method","Nodejs Post Method"],"articleSection":["Node Js Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/nodejs-rest-api\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/nodejs-rest-api\/","url":"https:\/\/data-flair.training\/blogs\/nodejs-rest-api\/","name":"Node.js Rest API - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-rest-api\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-rest-api\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-rest-api.webp","datePublished":"2021-12-18T03:30:21+00:00","dateModified":"2021-12-18T07:22:20+00:00","description":"Learn about nodejs Rest API. See REST Architecture, Web Services, HTTPS Methods, Get Method, Post Method, Delete Method etc.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-rest-api\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/nodejs-rest-api\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/nodejs-rest-api\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-rest-api.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/12\/nodejs-rest-api.webp","width":1200,"height":628,"caption":"nodejs rest api"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/nodejs-rest-api\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Node Js Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/node-js-tutorials\/"},{"@type":"ListItem","position":3,"name":"Node.js Rest API"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/104995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=104995"}],"version-history":[{"count":2,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/104995\/revisions"}],"predecessor-version":[{"id":105027,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/104995\/revisions\/105027"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/105023"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=104995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=104995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=104995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}