{"id":1752,"date":"2026-07-15T06:41:26","date_gmt":"2026-07-14T23:41:26","guid":{"rendered":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/15\/how-to-build-a-rest-api-with-node-js-a-step-by-step-guide\/"},"modified":"2026-07-15T06:41:27","modified_gmt":"2026-07-14T23:41:27","slug":"how-to-build-a-rest-api-with-node-js-a-step-by-step-guide","status":"publish","type":"post","link":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/15\/how-to-build-a-rest-api-with-node-js-a-step-by-step-guide\/","title":{"rendered":"How to Build a REST API with Node.js: A Step-by-Step Guide"},"content":{"rendered":"<h1>How to Build a REST API with Node.js: A Step-by-Step Guide<\/h1>\n<p>Node.js, combined with Express, is a powerful and lightweight choice for building REST APIs. This tutorial walks you through creating a basic API with standard CRUD operations, perfect for beginners or anyone needing a quick, scalable backend.<\/p>\n<p>Start by initializing a new project: run <code>npm init -y<\/code> in your terminal, then install Express with <code>npm install express<\/code>. Create a file named <code>index.js<\/code> for your server.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/sumberlaba.com\/wp-content\/uploads\/2026\/07\/article-1784072484782.jpg\" alt=\"Article illustration\" style=\"display:block;margin:20px auto;max-width:100%;height:auto;border-radius:8px;\" \/><\/p>\n<h2>1. Set Up Your Express Server<\/h2>\n<p>In <code>index.js<\/code>, require Express and create an app instance. Define a port and start listening:<\/p>\n<ul>\n<li><code>const express = require('express');<\/code><\/li>\n<li><code>const app = express();<\/code><\/li>\n<li><code>app.listen(3000, () => console.log('API running on port 3000'));<\/code><\/li>\n<\/ul>\n<h2>2. Define Routes and HTTP Methods<\/h2>\n<p>Use Express route handlers for CRUD operations. For a simple items resource:<\/p>\n<ul>\n<li><strong>GET<\/strong> <code>\/api\/items<\/code> \u2013 return array of items.<\/li>\n<li><strong>POST<\/strong> <code>\/api\/items<\/code> \u2013 add new item from request body.<\/li>\n<li><strong>PUT<\/strong> <code>\/api\/items\/:id<\/code> \u2013 update existing item.<\/li>\n<li><strong>DELETE<\/strong> <code>\/api\/items\/:id<\/code> \u2013 remove item.<\/li>\n<\/ul>\n<p>Example: <code>app.get('\/api\/items', (req, res) => res.json(items));<\/code><\/p>\n<h2>3. Add Middleware<\/h2>\n<p>Use built-in <code>express.json()<\/code> to parse JSON request bodies. Add error-handling middleware at the end:<\/p>\n<p><code>app.use(express.json());<\/code><br \/>\n<code>app.use((err, req, res, next) => { res.status(500).json({ error: err.message }); });<\/code><\/p>\n<h2>4. Test Your API<\/h2>\n<p>Run <code>node index.js<\/code> and test endpoints with Postman or curl. For example:<\/p>\n<ul>\n<li><code>curl http:\/\/localhost:3000\/api\/items<\/code><\/li>\n<li><code>curl -X POST -H \"Content-Type: application\/json\" -d '{\"name\":\"test\"}' http:\/\/localhost:3000\/api\/items<\/code><\/li>\n<\/ul>\n<p>Building a REST API with Node.js is straightforward. Start with the basics, then add databases like MongoDB, authentication, and validation. The key is to keep routes clean and middleware modular. Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Build a REST API with Node.js: A Step-by-Step Guide Node.js, combined with Express, is a powerful and lightweight choice for building REST APIs. This tutorial walks you through creating a basic API with standard CRUD operations, perfect for beginners or anyone needing a quick, scalable backend. Start by initializing a new project: run &hellip; <\/p>\n","protected":false},"author":2716,"featured_media":1751,"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-1752","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\/1752","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=1752"}],"version-history":[{"count":1,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/1752\/revisions"}],"predecessor-version":[{"id":1753,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/1752\/revisions\/1753"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/media\/1751"}],"wp:attachment":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/media?parent=1752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/categories?post=1752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/tags?post=1752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}