{"id":870,"date":"2025-01-25T19:38:00","date_gmt":"2025-01-25T19:38:00","guid":{"rendered":"https:\/\/gratisvps.net\/blog\/?p=870"},"modified":"2025-01-25T19:38:00","modified_gmt":"2025-01-25T19:38:00","slug":"how-to-build-a-simple-restful-api-node-js-tutorial","status":"publish","type":"post","link":"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/","title":{"rendered":"How To Build a Simple RESTful API: Node.js Tutorial"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#How_To_Build_a_Simple_RESTful_API_Nodejs_Tutorial\" >How To Build a Simple RESTful API: Node.js Tutorial<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#1_Introduction_to_Nodejs\" >1. Introduction to Node.js<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#Key_Features\" >Key Features:<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#2_Setting_Up_Your_Development_Environment\" >2. Setting Up Your Development Environment<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#Prerequisites\" >Prerequisites:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#Verify_Your_Installation\" >Verify Your Installation:<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#3_Creating_Your_First_Nodejs_Application\" >3. Creating Your First Node.js Application<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#4_Understanding_Expressjs\" >4. Understanding Express.js<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#Installing_Express\" >Installing Express:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#Creating_a_Simple_Express_App\" >Creating a Simple Express App:<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#5_Building_a_RESTful_API\" >5. Building a RESTful API<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#API_Structure\" >API Structure<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#Setting_Up_the_Project\" >Setting Up the Project:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#Update_indexjs\" >Update\u00a0index.js:<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#6_Connecting_to_a_MongoDB_Database\" >6. Connecting to a MongoDB Database<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#Install_Mongoose\" >Install Mongoose:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#Set_Up_Mongoose\" >Set Up Mongoose:<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#7_Testing_Your_API\" >7. Testing Your API<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#Using_Postman\" >Using Postman:<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#8_Deployment\" >8. Deployment<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#Deploying_on_Heroku\" >Deploying on Heroku:<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#9_FAQ\" >9. FAQ<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1 id=\"nodejs-tutorial-building-a-simple-restful-api\"><span class=\"ez-toc-section\" id=\"How_To_Build_a_Simple_RESTful_API_Nodejs_Tutorial\"><\/span>How To Build a Simple RESTful API: Node.js Tutorial<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n<hr \/>\n<h2 id=\"1-introduction-to-nodejs\"><span class=\"ez-toc-section\" id=\"1_Introduction_to_Nodejs\"><\/span>1. Introduction to Node.js<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Node.js is a runtime built on Chrome&#8217;s V8 JavaScript engine that allows you to execute JavaScript on the server side. It\u2019s designed for building scalable network <a href=\"https:\/\/gratisvps.net\/blog\/setting-up-a-web-application-stack-with-docker-on-ubuntu-vps\/\">applications<\/a> and is particularly well-suited for real-time web applications.<\/p>\n<h3 id=\"key-features\"><span class=\"ez-toc-section\" id=\"Key_Features\"><\/span>Key Features:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li><strong>Asynchronous and Event-Driven<\/strong>: Handles multiple requests simultaneously without blocking.<\/li>\n<li><strong>Fast Execution<\/strong>: V8 engine compiles JavaScript directly to native machine code.<\/li>\n<li><strong>NPM Ecosystem<\/strong>: A rich ecosystem of libraries and tools.<\/li>\n<\/ul>\n<h2 id=\"2-setting-up-your-development-environment\"><span class=\"ez-toc-section\" id=\"2_Setting_Up_Your_Development_Environment\"><\/span>2. Setting Up Your Development Environment<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3 id=\"prerequisites\"><span class=\"ez-toc-section\" id=\"Prerequisites\"><\/span>Prerequisites:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Install Node.js from\u00a0<a href=\"https:\/\/nodejs.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">nodejs.org<\/a>.<\/li>\n<li>Install MongoDB from\u00a0<a href=\"https:\/\/www.mongodb.com\/try\/download\/community\" target=\"_blank\" rel=\"noopener noreferrer\">mongodb.com<\/a>.<\/li>\n<li>A code editor like Visual Studio Code.<\/li>\n<\/ul>\n<h3 id=\"verify-your-installation\"><span class=\"ez-toc-section\" id=\"Verify_Your_Installation\"><\/span>Verify Your Installation:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"lang-bash hljs language-bash\">node -v  \r\nnpm -v<\/code><\/pre>\n<h2 id=\"3-creating-your-first-nodejs-application\"><span class=\"ez-toc-section\" id=\"3_Creating_Your_First_Nodejs_Application\"><\/span>3. Creating Your First Node.js Application<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Create a new directory for your project and navigate into it:<\/p>\n<pre><code class=\"lang-bash hljs language-bash\"><span class=\"hljs-built_in\">mkdir<\/span> my-node-app  \r\n<span class=\"hljs-built_in\">cd<\/span> my-node-app<\/code><\/pre>\n<p>Initialize your project:<\/p>\n<pre><code class=\"lang-bash hljs language-bash\">npm init -y<\/code><\/pre>\n<p>Create an\u00a0<code>index.js<\/code>\u00a0file:<\/p>\n<pre><code class=\"lang-javascript hljs language-javascript\"><span class=\"hljs-comment\">\/\/ index.js  <\/span>\r\n<span class=\"hljs-keyword\">const<\/span> http = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">'http'<\/span>);\r\n\r\n<span class=\"hljs-keyword\">const<\/span> server = http.<span class=\"hljs-title function_\">createServer<\/span>(<span class=\"hljs-function\">(<span class=\"hljs-params\">req, res<\/span>) =&gt;<\/span> {\r\n    res.<span class=\"hljs-property\">statusCode<\/span> = <span class=\"hljs-number\">200<\/span>;\r\n    res.<span class=\"hljs-title function_\">setHeader<\/span>(<span class=\"hljs-string\">'Content-Type'<\/span>, <span class=\"hljs-string\">'text\/plain'<\/span>);\r\n    res.<span class=\"hljs-title function_\">end<\/span>(<span class=\"hljs-string\">'Hello, World!\\n'<\/span>);\r\n});\r\n\r\nserver.<span class=\"hljs-title function_\">listen<\/span>(<span class=\"hljs-number\">3000<\/span>, <span class=\"hljs-function\">() =&gt;<\/span> {\r\n    <span class=\"hljs-variable language_\">console<\/span>.<span class=\"hljs-title function_\">log<\/span>(<span class=\"hljs-string\">'Server running at http:\/\/localhost:3000\/'<\/span>);\r\n});<\/code><\/pre>\n<p>Run your application:<\/p>\n<pre><code class=\"lang-bash hljs language-bash\">node index.js<\/code><\/pre>\n<p>Visit\u00a0<code>http:\/\/localhost:3000<\/code>\u00a0in your browser to see &#8220;Hello, World!&#8221;<\/p>\n<h2 id=\"4-understanding-expressjs\"><span class=\"ez-toc-section\" id=\"4_Understanding_Expressjs\"><\/span>4. Understanding Express.js<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Express.js is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.<\/p>\n<h3 id=\"installing-express\"><span class=\"ez-toc-section\" id=\"Installing_Express\"><\/span>Installing Express:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"lang-bash hljs language-bash\">npm install express<\/code><\/pre>\n<h3 id=\"creating-a-simple-express-app\"><span class=\"ez-toc-section\" id=\"Creating_a_Simple_Express_App\"><\/span>Creating a Simple Express App:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Replace the content of\u00a0<code>index.js<\/code>:<\/p>\n<pre><code class=\"lang-javascript hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> express = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">'express'<\/span>);\r\n<span class=\"hljs-keyword\">const<\/span> app = <span class=\"hljs-title function_\">express<\/span>();\r\n<span class=\"hljs-keyword\">const<\/span> port = <span class=\"hljs-number\">3000<\/span>;\r\n\r\napp.<span class=\"hljs-title function_\">get<\/span>(<span class=\"hljs-string\">'\/'<\/span>, <span class=\"hljs-function\">(<span class=\"hljs-params\">req, res<\/span>) =&gt;<\/span> {\r\n    res.<span class=\"hljs-title function_\">send<\/span>(<span class=\"hljs-string\">'Hello, Express!'<\/span>);\r\n});\r\n\r\napp.<span class=\"hljs-title function_\">listen<\/span>(port, <span class=\"hljs-function\">() =&gt;<\/span> {\r\n    <span class=\"hljs-variable language_\">console<\/span>.<span class=\"hljs-title function_\">log<\/span>(<span class=\"hljs-string\">`App running at http:\/\/localhost:<span class=\"hljs-subst\">${port}<\/span>`<\/span>);\r\n});<\/code><\/pre>\n<h2 id=\"5-building-a-restful-api\"><span class=\"ez-toc-section\" id=\"5_Building_a_RESTful_API\"><\/span>5. Building a RESTful API<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3 id=\"api-structure\"><span class=\"ez-toc-section\" id=\"API_Structure\"><\/span>API Structure<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>We will create a simple API for managing a list of users.<\/p>\n<h3 id=\"setting-up-the-project\"><span class=\"ez-toc-section\" id=\"Setting_Up_the_Project\"><\/span>Setting Up the Project:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Create a new file\u00a0<code>user.js<\/code>\u00a0to manage user data.<\/p>\n<pre><code class=\"lang-javascript hljs language-javascript\"><span class=\"hljs-comment\">\/\/ user.js  <\/span>\r\n<span class=\"hljs-keyword\">let<\/span> users = [];\r\n\r\n<span class=\"hljs-keyword\">const<\/span> <span class=\"hljs-title function_\">getUsers<\/span> = (<span class=\"hljs-params\">req, res<\/span>) =&gt; {\r\n    res.<span class=\"hljs-title function_\">json<\/span>(users);\r\n};\r\n\r\n<span class=\"hljs-keyword\">const<\/span> <span class=\"hljs-title function_\">addUser<\/span> = (<span class=\"hljs-params\">req, res<\/span>) =&gt; {\r\n    <span class=\"hljs-keyword\">const<\/span> user = req.<span class=\"hljs-property\">body<\/span>;\r\n    users.<span class=\"hljs-title function_\">push<\/span>(user);\r\n    res.<span class=\"hljs-title function_\">status<\/span>(<span class=\"hljs-number\">201<\/span>).<span class=\"hljs-title function_\">json<\/span>(user);\r\n};\r\n\r\n<span class=\"hljs-variable language_\">module<\/span>.<span class=\"hljs-property\">exports<\/span> = { getUsers, addUser };<\/code><\/pre>\n<h3 id=\"update-indexjs\"><span class=\"ez-toc-section\" id=\"Update_indexjs\"><\/span>Update\u00a0<code>index.js<\/code>:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"lang-javascript hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> express = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">'express'<\/span>);\r\n<span class=\"hljs-keyword\">const<\/span> bodyParser = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">'body-parser'<\/span>);\r\n<span class=\"hljs-keyword\">const<\/span> { getUsers, addUser } = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">'.\/user'<\/span>);\r\n\r\n<span class=\"hljs-keyword\">const<\/span> app = <span class=\"hljs-title function_\">express<\/span>();\r\n<span class=\"hljs-keyword\">const<\/span> port = <span class=\"hljs-number\">3000<\/span>;\r\n\r\napp.<span class=\"hljs-title function_\">use<\/span>(bodyParser.<span class=\"hljs-title function_\">json<\/span>());\r\n\r\napp.<span class=\"hljs-title function_\">get<\/span>(<span class=\"hljs-string\">'\/users'<\/span>, getUsers);\r\napp.<span class=\"hljs-title function_\">post<\/span>(<span class=\"hljs-string\">'\/users'<\/span>, addUser);\r\n\r\napp.<span class=\"hljs-title function_\">listen<\/span>(port, <span class=\"hljs-function\">() =&gt;<\/span> {\r\n    <span class=\"hljs-variable language_\">console<\/span>.<span class=\"hljs-title function_\">log<\/span>(<span class=\"hljs-string\">`API running at http:\/\/localhost:<span class=\"hljs-subst\">${port}<\/span>`<\/span>);\r\n});<\/code><\/pre>\n<h2 id=\"6-connecting-to-a-mongodb-database\"><span class=\"ez-toc-section\" id=\"6_Connecting_to_a_MongoDB_Database\"><\/span>6. Connecting to a MongoDB Database<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3 id=\"install-mongoose\"><span class=\"ez-toc-section\" id=\"Install_Mongoose\"><\/span>Install Mongoose:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"lang-bash hljs language-bash\">npm install mongoose<\/code><\/pre>\n<h3 id=\"set-up-mongoose\"><span class=\"ez-toc-section\" id=\"Set_Up_Mongoose\"><\/span>Set Up Mongoose:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Update\u00a0<code>user.js<\/code>\u00a0to use MongoDB.<\/p>\n<pre><code class=\"lang-javascript hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> mongoose = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">'mongoose'<\/span>);\r\n\r\nmongoose.<span class=\"hljs-title function_\">connect<\/span>(<span class=\"hljs-string\">'mongodb:\/\/localhost\/users'<\/span>, { <span class=\"hljs-attr\">useNewUrlParser<\/span>: <span class=\"hljs-literal\">true<\/span>, <span class=\"hljs-attr\">useUnifiedTopology<\/span>: <span class=\"hljs-literal\">true<\/span> });\r\n\r\n<span class=\"hljs-keyword\">const<\/span> userSchema = <span class=\"hljs-keyword\">new<\/span> mongoose.<span class=\"hljs-title class_\">Schema<\/span>({\r\n    <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-title class_\">String<\/span>,\r\n    <span class=\"hljs-attr\">email<\/span>: <span class=\"hljs-title class_\">String<\/span>,\r\n});\r\n\r\n<span class=\"hljs-keyword\">const<\/span> <span class=\"hljs-title class_\">User<\/span> = mongoose.<span class=\"hljs-title function_\">model<\/span>(<span class=\"hljs-string\">'User'<\/span>, userSchema);\r\n\r\n<span class=\"hljs-keyword\">const<\/span> <span class=\"hljs-title function_\">getUsers<\/span> = <span class=\"hljs-keyword\">async<\/span> (<span class=\"hljs-params\">req, res<\/span>) =&gt; {\r\n    <span class=\"hljs-keyword\">const<\/span> users = <span class=\"hljs-keyword\">await<\/span> <span class=\"hljs-title class_\">User<\/span>.<span class=\"hljs-title function_\">find<\/span>();\r\n    res.<span class=\"hljs-title function_\">json<\/span>(users);\r\n};\r\n\r\n<span class=\"hljs-keyword\">const<\/span> <span class=\"hljs-title function_\">addUser<\/span> = <span class=\"hljs-keyword\">async<\/span> (<span class=\"hljs-params\">req, res<\/span>) =&gt; {\r\n    <span class=\"hljs-keyword\">const<\/span> user = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">User<\/span>(req.<span class=\"hljs-property\">body<\/span>);\r\n    <span class=\"hljs-keyword\">await<\/span> user.<span class=\"hljs-title function_\">save<\/span>();\r\n    res.<span class=\"hljs-title function_\">status<\/span>(<span class=\"hljs-number\">201<\/span>).<span class=\"hljs-title function_\">json<\/span>(user);\r\n};<\/code><\/pre>\n<h2 id=\"7-testing-your-api\"><span class=\"ez-toc-section\" id=\"7_Testing_Your_API\"><\/span>7. Testing Your API<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3 id=\"using-postman\"><span class=\"ez-toc-section\" id=\"Using_Postman\"><\/span>Using Postman:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ol start=\"1\">\n<li>Open Postman.<\/li>\n<li>To get users, send a GET request to\u00a0<code>http:\/\/localhost:3000\/users<\/code>.<\/li>\n<li>To add a user, send a POST request with JSON body to\u00a0<code>http:\/\/localhost:3000\/users<\/code>.<\/li>\n<\/ol>\n<p>Example JSON body:\u00a0<code>json { \"name\": \"John Doe\", \"email\": \"john@example.com\" }<\/code><\/p>\n<h2 id=\"8-deployment\"><span class=\"ez-toc-section\" id=\"8_Deployment\"><\/span>8. Deployment<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3 id=\"deploying-on-heroku\"><span class=\"ez-toc-section\" id=\"Deploying_on_Heroku\"><\/span>Deploying on Heroku:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ol start=\"1\">\n<li>Install the Heroku CLI.<\/li>\n<li>Run\u00a0<code>heroku login<\/code>\u00a0and create a new app with\u00a0<code>heroku create<\/code>.<\/li>\n<li>Add MongoDB add-on:\u00a0<code>heroku addons:create mongolab<\/code>.<\/li>\n<li>Push your code:\u00a0<code>git push heroku master<\/code>.<\/li>\n<li>Open your app:\u00a0<code>heroku open<\/code>.<\/li>\n<\/ol>\n<h2 id=\"9-faq\"><span class=\"ez-toc-section\" id=\"9_FAQ\"><\/span>9. FAQ<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><strong>Q1: What is Node.js good for?<\/strong><br \/>\nA1: Node.js is excellent for building fast, scalable network applications, especially real-time applications like chat apps, collaborative tools, and REST APIs.<\/p>\n<p><strong>Q2: Is Node.js suitable for enterprise applications?<\/strong><br \/>\nA2: Yes, several large companies, like Netflix and LinkedIn, use Node.js for their backend, thanks to its scalability and performance.<\/p>\n<p><strong>Q3: Can I use Node.js for front-end development?<\/strong><br \/>\nA3: Node.js is primarily a backend technology, but tools like React, Angular, or Vue can be used with Node.js as a build tool.<\/p>\n<p><strong>Q4: What are some alternatives to Node.js?<\/strong><br \/>\nA4: Alternatives include Python (Flask, Django), Ruby on Rails, and Java (Spring).<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How To Build a Simple RESTful API: Node.js Tutorial 1. Introduction to Node.js Node.js is a runtime built on Chrome&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":872,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[274,215],"tags":[276,275],"class_list":["post-870","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js","category-tutorial","tag-node-js","tag-restful-api"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.4 (Yoast SEO v27.7) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How To Build a Simple RESTful API: Node.js Tutorial<\/title>\n<meta name=\"description\" content=\"How To Build a Simple RESTful API: Node.js Advanced Tutorial , This article will teach you how to build a restful api in node.js\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Build a Simple RESTful API: Node.js Tutorial\" \/>\n<meta property=\"og:description\" content=\"How To Build a Simple RESTful API: Node.js Advanced Tutorial , This article will teach you how to build a restful api in node.js\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"Free VPS Hosting Guides\" \/>\n<meta property=\"article:published_time\" content=\"2025-01-25T19:38:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/gratisvps.net\/blog\/wp-content\/uploads\/2025\/01\/node-js.png\" \/>\n\t<meta property=\"og:image:width\" content=\"832\" \/>\n\t<meta property=\"og:image:height\" content=\"448\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"ariete\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"ariete\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/how-to-build-a-simple-restful-api-node-js-tutorial\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/how-to-build-a-simple-restful-api-node-js-tutorial\\\/\"},\"author\":{\"name\":\"ariete\",\"@id\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/#\\\/schema\\\/person\\\/cddcf8cb5192d0713c19b79425c77fc4\"},\"headline\":\"How To Build a Simple RESTful API: Node.js Tutorial\",\"datePublished\":\"2025-01-25T19:38:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/how-to-build-a-simple-restful-api-node-js-tutorial\\\/\"},\"wordCount\":406,\"publisher\":{\"@id\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/how-to-build-a-simple-restful-api-node-js-tutorial\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/01\\\/node-js.png\",\"keywords\":[\"Node.js\",\"RESTful API\"],\"articleSection\":[\"Node.js\",\"Tutorial\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/how-to-build-a-simple-restful-api-node-js-tutorial\\\/\",\"url\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/how-to-build-a-simple-restful-api-node-js-tutorial\\\/\",\"name\":\"How To Build a Simple RESTful API: Node.js Tutorial\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/how-to-build-a-simple-restful-api-node-js-tutorial\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/how-to-build-a-simple-restful-api-node-js-tutorial\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/01\\\/node-js.png\",\"datePublished\":\"2025-01-25T19:38:00+00:00\",\"description\":\"How To Build a Simple RESTful API: Node.js Advanced Tutorial , This article will teach you how to build a restful api in node.js\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/how-to-build-a-simple-restful-api-node-js-tutorial\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gratisvps.net\\\/blog\\\/how-to-build-a-simple-restful-api-node-js-tutorial\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/how-to-build-a-simple-restful-api-node-js-tutorial\\\/#primaryimage\",\"url\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/01\\\/node-js.png\",\"contentUrl\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/01\\\/node-js.png\",\"width\":832,\"height\":448,\"caption\":\"RESTful API\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/how-to-build-a-simple-restful-api-node-js-tutorial\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How To Build a Simple RESTful API: Node.js Tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/\",\"name\":\"Gratisvps.net | Blog Daily Tech Info\",\"description\":\"GratisVPS.NET\",\"publisher\":{\"@id\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/#organization\",\"name\":\"Gratisvps.net | Blog Daily Tech Info\",\"url\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/10\\\/logo.png\",\"contentUrl\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/wp-content\\\/uploads\\\/2024\\\/10\\\/logo.png\",\"width\":250,\"height\":67,\"caption\":\"Gratisvps.net | Blog Daily Tech Info\"},\"image\":{\"@id\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/#\\\/schema\\\/person\\\/cddcf8cb5192d0713c19b79425c77fc4\",\"name\":\"ariete\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b93881052caa63fd6b2fb5468a80afcf9f985a165c6d4de11a72cc4c0775f74a?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b93881052caa63fd6b2fb5468a80afcf9f985a165c6d4de11a72cc4c0775f74a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b93881052caa63fd6b2fb5468a80afcf9f985a165c6d4de11a72cc4c0775f74a?s=96&d=mm&r=g\",\"caption\":\"ariete\"},\"sameAs\":[\"https:\\\/\\\/gratisvps.net\\\/blog\"],\"url\":\"https:\\\/\\\/gratisvps.net\\\/blog\\\/author\\\/ariete\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How To Build a Simple RESTful API: Node.js Tutorial","description":"How To Build a Simple RESTful API: Node.js Advanced Tutorial , This article will teach you how to build a restful api in node.js","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:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/","og_locale":"en_US","og_type":"article","og_title":"How To Build a Simple RESTful API: Node.js Tutorial","og_description":"How To Build a Simple RESTful API: Node.js Advanced Tutorial , This article will teach you how to build a restful api in node.js","og_url":"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/","og_site_name":"Free VPS Hosting Guides","article_published_time":"2025-01-25T19:38:00+00:00","og_image":[{"width":832,"height":448,"url":"https:\/\/gratisvps.net\/blog\/wp-content\/uploads\/2025\/01\/node-js.png","type":"image\/png"}],"author":"ariete","twitter_card":"summary_large_image","twitter_misc":{"Written by":"ariete","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#article","isPartOf":{"@id":"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/"},"author":{"name":"ariete","@id":"https:\/\/gratisvps.net\/blog\/#\/schema\/person\/cddcf8cb5192d0713c19b79425c77fc4"},"headline":"How To Build a Simple RESTful API: Node.js Tutorial","datePublished":"2025-01-25T19:38:00+00:00","mainEntityOfPage":{"@id":"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/"},"wordCount":406,"publisher":{"@id":"https:\/\/gratisvps.net\/blog\/#organization"},"image":{"@id":"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/gratisvps.net\/blog\/wp-content\/uploads\/2025\/01\/node-js.png","keywords":["Node.js","RESTful API"],"articleSection":["Node.js","Tutorial"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/","url":"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/","name":"How To Build a Simple RESTful API: Node.js Tutorial","isPartOf":{"@id":"https:\/\/gratisvps.net\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/gratisvps.net\/blog\/wp-content\/uploads\/2025\/01\/node-js.png","datePublished":"2025-01-25T19:38:00+00:00","description":"How To Build a Simple RESTful API: Node.js Advanced Tutorial , This article will teach you how to build a restful api in node.js","breadcrumb":{"@id":"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#primaryimage","url":"https:\/\/gratisvps.net\/blog\/wp-content\/uploads\/2025\/01\/node-js.png","contentUrl":"https:\/\/gratisvps.net\/blog\/wp-content\/uploads\/2025\/01\/node-js.png","width":832,"height":448,"caption":"RESTful API"},{"@type":"BreadcrumbList","@id":"https:\/\/gratisvps.net\/blog\/how-to-build-a-simple-restful-api-node-js-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gratisvps.net\/blog\/"},{"@type":"ListItem","position":2,"name":"How To Build a Simple RESTful API: Node.js Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/gratisvps.net\/blog\/#website","url":"https:\/\/gratisvps.net\/blog\/","name":"Gratisvps.net | Blog Daily Tech Info","description":"GratisVPS.NET","publisher":{"@id":"https:\/\/gratisvps.net\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gratisvps.net\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/gratisvps.net\/blog\/#organization","name":"Gratisvps.net | Blog Daily Tech Info","url":"https:\/\/gratisvps.net\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/gratisvps.net\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/gratisvps.net\/blog\/wp-content\/uploads\/2024\/10\/logo.png","contentUrl":"https:\/\/gratisvps.net\/blog\/wp-content\/uploads\/2024\/10\/logo.png","width":250,"height":67,"caption":"Gratisvps.net | Blog Daily Tech Info"},"image":{"@id":"https:\/\/gratisvps.net\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/gratisvps.net\/blog\/#\/schema\/person\/cddcf8cb5192d0713c19b79425c77fc4","name":"ariete","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b93881052caa63fd6b2fb5468a80afcf9f985a165c6d4de11a72cc4c0775f74a?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b93881052caa63fd6b2fb5468a80afcf9f985a165c6d4de11a72cc4c0775f74a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b93881052caa63fd6b2fb5468a80afcf9f985a165c6d4de11a72cc4c0775f74a?s=96&d=mm&r=g","caption":"ariete"},"sameAs":["https:\/\/gratisvps.net\/blog"],"url":"https:\/\/gratisvps.net\/blog\/author\/ariete\/"}]}},"_links":{"self":[{"href":"https:\/\/gratisvps.net\/blog\/wp-json\/wp\/v2\/posts\/870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gratisvps.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gratisvps.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gratisvps.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gratisvps.net\/blog\/wp-json\/wp\/v2\/comments?post=870"}],"version-history":[{"count":2,"href":"https:\/\/gratisvps.net\/blog\/wp-json\/wp\/v2\/posts\/870\/revisions"}],"predecessor-version":[{"id":873,"href":"https:\/\/gratisvps.net\/blog\/wp-json\/wp\/v2\/posts\/870\/revisions\/873"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gratisvps.net\/blog\/wp-json\/wp\/v2\/media\/872"}],"wp:attachment":[{"href":"https:\/\/gratisvps.net\/blog\/wp-json\/wp\/v2\/media?parent=870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gratisvps.net\/blog\/wp-json\/wp\/v2\/categories?post=870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gratisvps.net\/blog\/wp-json\/wp\/v2\/tags?post=870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}