One of the most sought-after abilities in contemporary web development is creating a REST API with Node.js and Express. REST APIs enable HTTP communication between various applications (such as web apps, mobile apps, and services). While Express is a lightweight framework that makes API development easy and effective, Node.js offers a quick and scalable runtime. They are frequently utilized in backend development together. Using real-world examples and best practices, this step-by-step tutorial will teach you how to create a REST API with Node.js and Express.

What is a REST API?
A REST API (Representational State Transfer API) is a way to communicate between client and server using standard HTTP methods.
Common HTTP Methods
- GET: Fetch data
- POST: Create new data
- PUT: Update existing data
- DELETE: Remove data
Example
A simple API for users:
- GET /users → Get all users
- POST /users → Create a user
- PUT /users/1 → Update user
- DELETE /users/1 → Delete user
Step 1: Install Node.js and Setup Project
First, install Node.js from the official website.
Then create a new project:
mkdir rest-api
cd rest-api
npm init -y
This creates a package.json file.
Step 2: Install Express
Install Express framework:
npm install express
Express helps in routing and handling HTTP requests easily.
Step 3: Create Basic Server
Create a file named server.js:
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
res.send('API is running');
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
Run the server:
node server.js
Open browser: http://localhost:3000
Step 4: Use Middleware (JSON Parsing)
To handle JSON data, use built-in middleware:
app.use(express.json());
This allows your API to read JSON from request body.
Step 5: Create Routes
Now create basic CRUD routes.
let users = [];
// GET users
app.get('/users', (req, res) => {
res.json(users);
});
// POST user
app.post('/users', (req, res) => {
const user = req.body;
users.push(user);
res.status(201).json(user);
});
Step 6: Add PUT and DELETE Routes
// PUT update user
app.put('/users/:id', (req, res) => {
const id = parseInt(req.params.id);
users[id] = req.body;
res.json(users[id]);
});
// DELETE user
app.delete('/users/:id', (req, res) => {
const id = parseInt(req.params.id);
users.splice(id, 1);
res.send('User deleted');
});
Step 7: Use Express Router (Better Structure)
Create a routes file:
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
res.send('Users route');
});
module.exports = router;
Use it in server:
const userRoutes = require('./routes/users');
app.use('/users', userRoutes);
Step 8: Connect to Database (Optional)
In real projects, data is stored in databases like MongoDB or MySQL.
Example with MongoDB (conceptual):
// connect to database and store users
Step 9: Error Handling
Handle errors properly:
app.use((err, req, res, next) => {
res.status(500).json({ message: err.message });
});
Step 10: Test API
You can test APIs using:
- Postman
- Thunder Client
- Browser (for GET)
Example request:
POST /users
{
"name": "John",
"age": 25
}
Best Practices for REST API Development
- Use proper HTTP methods
- Keep routes clean and meaningful
- Validate user input
- Use status codes correctly
- Structure project properly
Common Mistakes Developers Make
- Not validating input
- Mixing business logic in routes
- Ignoring error handling
- Poor route naming
Real-World Use Cases
- Backend for web applications
- Mobile app APIs
- E-commerce platforms
- Authentication systems
Summary
Express and Node.js make it easy and effective to create a REST API. You may develop scalable and maintainable APIs for contemporary apps by taking a methodical approach. Express simplifies backend development with features like JSON handling, middleware, and routing. Building effective and production-ready APIs requires an understanding of REST principles and best practices.