{"id":2858,"date":"2026-08-07T00:02:04","date_gmt":"2026-08-06T17:02:04","guid":{"rendered":"https:\/\/sumberlaba.com\/index.php\/2026\/08\/07\/how-to-build-a-restful-api-with-express-a-step-by-step-guide\/"},"modified":"2026-08-07T00:02:04","modified_gmt":"2026-08-06T17:02:04","slug":"how-to-build-a-restful-api-with-express-a-step-by-step-guide","status":"publish","type":"post","link":"https:\/\/sumberlaba.com\/index.php\/2026\/08\/07\/how-to-build-a-restful-api-with-express-a-step-by-step-guide\/","title":{"rendered":"How to Build a RESTful API with Express: A Step-by-Step Guide"},"content":{"rendered":"<h1>How to Build a RESTful API with Express: A Step-by-Step Guide<\/h1>\n<p>Express is the most popular framework for building RESTful APIs with Node.js. It is lightweight, fast, and offers a clean way to handle HTTP requests. In this tutorial, you&#8217;ll learn how to build a practical API from scratch, covering routing, CRUD operations, and middleware.<\/p>\n<p>Before we start, ensure you have Node.js and npm installed on your machine. Basic knowledge of JavaScript and REST conventions (GET, POST, PUT, DELETE) will help you follow along smoothly.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/via.placeholder.com\/800x600\/4a90d9\/ffffff?text=how%20to%20build%20a%20restful%20api%20with%20express\" alt=\"Article illustration\" style=\"display:block;margin:20px auto;max-width:100%;height:auto;border-radius:8px;\" \/><\/p>\n<h2>1. Project Setup<\/h2>\n<p>Create a new directory and initialize your project with the following commands:<\/p>\n<ul>\n<li><code>npm init -y<\/code> \u2013 creates a <code>package.json<\/code> file.<\/li>\n<li><code>npm install express<\/code> \u2013 installs the Express framework.<\/li>\n<li><code>npm install --save-dev nodemon<\/code> \u2013 auto-restarts your server during development.<\/li>\n<\/ul>\n<h2>2. Create the Server and Initial Route<\/h2>\n<p>Create an <code>index.js<\/code> file with a basic Express server:<\/p>\n<pre><code>const express = require('express');\nconst app = express();\napp.use(express.json()); \/\/ Parse JSON bodies\n\napp.get('\/', (req, res) =&gt; res.send('API is running'));\napp.listen(3000, () =&gt; console.log('Server on port 3000'));<\/code><\/pre>\n<h2>3. Implement CRUD Operations<\/h2>\n<p>Create an in-memory array as your data store, then define the API endpoints:<\/p>\n<pre><code>let items = [{ id: 1, name: 'Apple' }];\n\n\/\/ GET all items\napp.get('\/api\/items', (req, res) =&gt; res.json(items));\n\n\/\/ GET a single item by ID\napp.get('\/api\/items\/:id', (req, res) =&gt; {\n  const item = items.find(i =&gt; i.id === parseInt(req.params.id));\n  item ? res.json(item) : res.status(404).send('Not found');\n});\n\n\/\/ POST a new item\napp.post('\/api\/items', (req, res) =&gt; {\n  const newItem = { id: items.length + 1, name: req.body.name };\n  items.push(newItem);\n  res.status(201).json(newItem);\n});\n\n\/\/ PUT: update an existing item\napp.put('\/api\/items\/:id', (req, res) =&gt; {\n  const item = items.find(i =&gt; i.id === parseInt(req.params.id));\n  if (!item) return res.status(404).send('Not found');\n  item.name = req.body.name;\n  res.json(item);\n});\n\n\/\/ DELETE an item\napp.delete('\/api\/items\/:id', (req, res) =&gt; {\n  items = items.filter(i =&gt; i.id !== parseInt(req.params.id));\n  res.status(204).send();\n});<\/code><\/pre>\n<h2>4. Test Your API<\/h2>\n<p>Run <code>npm start<\/code> (with nodemon) and test the endpoints using Postman, curl, or your browser. Use <code>express.json()<\/code> middleware to handle request bodies and consider adding morgan for request logging.<\/p>\n<p><strong>Conclusion:<\/strong> You&#8217;ve built a functional RESTful API with Express in just a few steps. From here, you can connect a real database, add authentication, and structure your routes with Express Router for larger projects.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Build a RESTful API with Express: A Step-by-Step Guide Express is the most popular framework for building RESTful APIs with Node.js. It is lightweight, fast, and offers a clean way to handle HTTP requests. In this tutorial, you&#8217;ll learn how to build a practical API from scratch, covering routing, CRUD operations, and middleware. &hellip; <\/p>\n","protected":false},"author":2716,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-2858","post","type-post","status-publish","format-standard","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/2858","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=2858"}],"version-history":[{"count":0,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/2858\/revisions"}],"wp:attachment":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/media?parent=2858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/categories?post=2858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/tags?post=2858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}