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.
- Why Use Node.js and Express for APIs?
- Step 1: Initialize a Node.js Project
- Step 2: Create Basic Project Structure
- Step 3: Create Your Express Server
- Step 4: Create Sample API Routes (CRUD)
- Sample Data
- 🔹 GET – Fetch All Users
- 🔹 GET – Fetch User by ID
- 🔹 POST – Create a New User
- 🔹 PUT – Update a User
- 🔹 DELETE – Remove a User
- 🧪 Step 5: Test Your API
- Step 6: Add Basic Error Handling Middleware
- Step 8: Organize Code (Recommended)
- Step 9: Prepare for Database Integration
- 🚀 Step 10: Deploy Your REST API
- Frequently Asked QuestionsIs Express still relevant in 2025?
- Should I use TypeScript?
- Is Node.js good for large apps?
🧰 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.jsand 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());
REST APIs are the backbone of nearly all modern web and mobile applications. Whether it’s a login system, dashboard, or mobile app, Node.js with Express is still one of the most popular ways to create high-performance REST APIs in 2025.
In this tutorial, you will learn how to create a simple REST API from scratch and learn how it works.
Why Use Node.js and Express for APIs?
Node.js + Express is popular because:
- Fast and lightweight
- JavaScript everywhere (frontend + backend)
- Huge ecosystem (npm)
- Easy to learn
- Great for real-time apps
Express simplifies routing, middleware, and request handling.
Step 1: Initialize a Node.js Project
Create a new folder and initialize your project.
mkdir simple-rest-api
cd simple-rest-api
npm init -y
Install Express:
npm install express
Optional but recommended (for auto-restart):
npm install nodemon --save-dev
Step 2: Create Basic Project Structure
simple-rest-api/
│
├── index.js
├── package.json
└── node_modules/
Step 3: Create Your Express Server
Create index.js and add:
const express = require("express");
const app = express();
const PORT = 3000;
// Middleware
app.use(express.json());
// Root route
app.get("/", (req, res) => {
res.json({ message: "API is running 🚀" });
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
Run the server:
node index.js
Or using nodemon:
npx nodemon index.js
Step 4: Create Sample API Routes (CRUD)
We’ll create a simple in-memory users API.
Sample Data
let users = [
{ id: 1, name: "Alice" },
{ id: 2, name: "Bob" }
];
🔹 GET – Fetch All Users
app.get("/users", (req, res) => {
res.json(users);
});
🔹 GET – Fetch User by ID
app.get("/users/:id", (req, res) => {
const user = users.find(u => u.id === parseInt(req.params.id));
if (!user) {
return res.status(404).json({ error: "User not found" });
}
res.json(user);
});
🔹 POST – Create a New User
app.post("/users", (req, res) => {
const { name } = req.body;
if (!name) {
return res.status(400).json({ error: "Name is required" });
}
const newUser = {
id: users.length + 1,
name
};
users.push(newUser);
res.status(201).json(newUser);
});
🔹 PUT – Update a User
app.put("/users/:id", (req, res) => {
const user = users.find(u => u.id === parseInt(req.params.id));
if (!user) {
return res.status(404).json({ error: "User not found" });
}
user.name = req.body.name || user.name;
res.json(user);
});
🔹 DELETE – Remove a User
app.delete("/users/:id", (req, res) => {
users = users.filter(u => u.id !== parseInt(req.params.id));
res.json({ message: "User deleted successfully" });
});
🧪 Step 5: Test Your API
Use:
- Postman
- Thunder Client
- curl
Example Test
GET http://localhost:3000/users
Step 6: Add Basic Error Handling Middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: "Something went wrong" });
});
Step 7: Security Best Practices (2025)
Even simple APIs should follow security basics.
✅ Essential Security Tips
Validate request data
Never trust client input
Use HTTPS in production
Hide environment variables
Add rate limiting
Enable CORS carefully
Optional packages:
npm install cors helmet express-rate-limit
Step 8: Organize Code (Recommended)
As your API grows, split files:
├── routes/
│ └── users.js
├── controllers/
├── middleware/
├── index.js
This improves maintainability and scalability.
Step 9: Prepare for Database Integration
In real projects, you’ll replace in-memory data with:
- MongoDB (Mongoose)
- PostgreSQL
- MySQL
Your API logic remains mostly the same.
🚀 Step 10: Deploy Your REST API
Free & popular platforms:
- Render
- Railway
- Fly.io
- Vercel (serverless)
Set environment variables properly before deployment.
Frequently Asked Questions
Is Express still relevant in 2025?
Yes. Express remains lightweight, stable, and widely used.
Should I use TypeScript?
Highly recommended for production APIs.
Is Node.js good for large apps?
Yes, with proper architecture and scaling.
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