Building a Simple REST API with Node.js
Sep 27, 2024
In this guide, we’ll create a basic REST API using Node.js and Express.
Get Smalery Suffy’s stories in your inbox
Join Medium for free to get updates from this writer.
Setup:
- Create a new directory:
mkdir my-api && cd my-api - Initialize the project:
npm init -y - Install Express:
npm install express
Create server.js:
javascriptCopy codeconst express = require('express');
const app = express();
const PORT = 3000;app.use(express.json());
app.get('/api/items', (req, res) => res.json([{ id: 1, name: 'Item 1' }]));
app.listen(PORT, () => console.log(`Server running on http://localhost:${PORT}`));Test with Postman using GET /api/items. Happy coding!




