Building a REST API is a key skill for developers in 2025. It doesn’t matter if you’re working on a web app, backend for mobile, or microservices – a well-designed API speeds up development and helps your project grow. This guide will show you how to create a basic REST API with Node.js and Express.js even if you’re just starting out.
๐งฐ Things You Need
- Node.js on your computer (Get it here)
- A grasp of JavaScript basics
- VS Code or another code editor
- Postman or a similar tool to test APIs
๐ Step 1: Initialize Your Project
Open your terminal and run:
mkdir simple-rest-api
cd simple-rest-api
npm init -y
This creates a basic package.json
file.
๐ฆ Step 2: Install Express
- Install Express.js:
npm install express
๐ Step 3: Create Your API Server
- Create a file called
index.js
and paste this code:
const express = require('express');
const app = express();
const PORT = 3000;
app.use(express.json());
let users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' }
];
// GET all users
app.get('/users', (req, res) => {
res.json(users);
});
// GET a single user
app.get('/users/:id', (req, res) => {
const user = users.find(u => u.id === parseInt(req.params.id));
user ? res.json(user) : res.status(404).send('User not found');
});
// POST a new user
app.post('/users', (req, res) => {
const newUser = {
id: users.length + 1,
name: req.body.name
};
users.push(newUser);
res.status(201).json(newUser);
});
// DELETE a user
app.delete('/users/:id', (req, res) => {
users = users.filter(u => u.id !== parseInt(req.params.id));
res.send('User deleted');
});
app.listen(PORT, () => console.log(`Server running on http://localhost:${PORT}`));

๐งช Step 4: Test Your API
Use Postman or Hoppscotch to test these endpoints:
DELETE /users/:id
โ Delete a user
GET /users
โ List all users
GET /users/:id
โ Get user by ID
POST /users
โ Add new user (JSON body:{ "name": "John" }
)
๐ Step 5: Add CORS (Optional)
To allow cross-origin requests, install and enable CORS:
npm install cors
Then add to your code:
const cors = require('cors');
app.use(cors());
Congratulations! You have just developed your first working REST API using Node.js and Express. It is a basic version, but it is a scalable structure โ you can add middleware, authentication, and a database! Please check back for our next guide on connecting to MongoDB.More Posts on Flymingotech.in