{"id":1536,"date":"2026-07-13T18:27:09","date_gmt":"2026-07-13T11:27:09","guid":{"rendered":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/13\/how-to-build-a-rest-api-with-node-js-a-step-by-step-tutorial\/"},"modified":"2026-07-13T18:27:10","modified_gmt":"2026-07-13T11:27:10","slug":"how-to-build-a-rest-api-with-node-js-a-step-by-step-tutorial","status":"publish","type":"post","link":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/13\/how-to-build-a-rest-api-with-node-js-a-step-by-step-tutorial\/","title":{"rendered":"How to Build a REST API with Node.js: A Step-by-Step Tutorial"},"content":{"rendered":"<h1>How to Build a REST API with Node.js: A Step-by-Step Tutorial<\/h1>\n<p>Node.js, combined with Express, makes building a REST API fast and lightweight. In this tutorial, you&#8217;ll create a simple CRUD API for managing items, covering routes, JSON handling, and testing with a HTTP client.<\/p>\n<h2>Prerequisites and Project Setup<\/h2>\n<p>Ensure Node.js (v14+) and npm are installed. Create a new project folder and initialize it:<\/p>\n<ul>\n<li><code>mkdir my-rest-api &amp;&amp; cd my-rest-api<\/code><\/li>\n<li><code>npm init -y<\/code><\/li>\n<li>Install Express: <code>npm install express<\/code><\/li>\n<\/ul>\n<p>Create a file <code>server.js<\/code> \u2013 this will house your entire API logic.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/sumberlaba.com\/wp-content\/uploads\/2026\/07\/article-1783942028326.jpg\" alt=\"Article illustration\" style=\"display:block;margin:20px auto;max-width:100%;height:auto;border-radius:8px;\" \/><\/p>\n<h2>Writing the Server and Basic Route<\/h2>\n<p>Start with a basic Express server and a health-check endpoint. Open <code>server.js<\/code> and add:<\/p>\n<ul>\n<li><code>const express = require('express'); const app = express();<\/code><\/li>\n<li>Use <code>express.json()<\/code> middleware to parse JSON bodies.<\/li>\n<li>Define <code>app.get('\/api\/items', (req, res) =&gt; { ... })<\/code> that returns a hardcoded array.<\/li>\n<li>Listen on port 3000 and log a startup message.<\/li>\n<\/ul>\n<p>Run the server with <code>node server.js<\/code>. Visit <code>http:\/\/localhost:3000\/api\/items<\/code> \u2013 you should see an empty JSON array or your sample data.<\/p>\n<h2>Implementing CRUD Operations<\/h2>\n<p>Now expand your API with Create, Read, Update, and Delete endpoints using an in-memory array for simplicity.<\/p>\n<h3>Get All Items and Get Single Item<\/h3>\n<ul>\n<li><code>GET \/api\/items<\/code> \u2013 returns all items.<\/li>\n<li><code>GET \/api\/items\/:id<\/code> \u2013 finds item by ID and returns it, or sends 404 if not found.<\/li>\n<\/ul>\n<h3>Create, Update, and Delete<\/h3>\n<ul>\n<li><code>POST \/api\/items<\/code> \u2013 reads <code>req.body<\/code>, creates a new item with a unique ID (use a counter), and responds with 201 status.<\/li>\n<li><code>PUT \/api\/items\/:id<\/code> \u2013 updates item fields from <code>req.body<\/code>, returns updated item or 404.<\/li>\n<li><code>DELETE \/api\/items\/:id<\/code> \u2013 removes item from array, returns 204 No Content or 404.<\/li>\n<\/ul>\n<p>Test each endpoint using curl, Postman, or any REST client.<\/p>\n<h2>Best Practices and Next Steps<\/h2>\n<p>To make your API production-ready:<\/p>\n<ul>\n<li>Validate input (e.g., check for required fields).<\/li>\n<li>Use environment variables (via <code>dotenv<\/code>) for port and database URLs.<\/li>\n<li>Add error handling middleware.<\/li>\n<li>Use a real database like MongoDB or PostgreSQL with an ODM\/ORM (Mongoose, Sequelize).<\/li>\n<\/ul>\n<p>You&#8217;ve now built a functional REST API with Node.js and Express in just a few steps.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Build a REST API with Node.js: A Step-by-Step Tutorial Node.js, combined with Express, makes building a REST API fast and lightweight. In this tutorial, you&#8217;ll create a simple CRUD API for managing items, covering routes, JSON handling, and testing with a HTTP client. Prerequisites and Project Setup Ensure Node.js (v14+) and npm are &hellip; <\/p>\n","protected":false},"author":2716,"featured_media":1535,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1536","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-non-category"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/1536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/users\/2716"}],"replies":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/comments?post=1536"}],"version-history":[{"count":1,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/1536\/revisions"}],"predecessor-version":[{"id":1537,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/1536\/revisions\/1537"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/media\/1535"}],"wp:attachment":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/media?parent=1536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/categories?post=1536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/tags?post=1536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}