A 2023 Stack Overflow developer survey revealed that over 52% of developers prefer Node.js for building backend services. Among those, Express remains one of the most commonly used frameworks due to its minimal setup and flexibility. So, if you’re looking to build a REST API quickly and efficiently, Node.js with Express is a go-to stack.
In this tutorial, we’ll walk you through building a simple yet practical REST API from scratch using Node.js and Express, explain each component in a beginner-friendly manner, and explore best practices that apply to real-world projects.
🔧 What You’ll Build
A basic REST API that manages a list of books. You’ll be able to:
- Get all books
- Get a single book by ID
- Add a new book
- Update a book
- Delete a book
🧰 Prerequisites
Before we begin, make sure you have the following installed:
- Node.js (v14 or above)
- npm (Node Package Manager)
- A code editor (like VS Code)
- Postman or any REST client (for testing)
🗂️ Step 1: Initialize Your Project
mkdir books-api
cd books-api
npm init -y
Now install Express:
npm install express
📁 Step 2: Create Your Server File
Create a file named server.js
:
const express = require('express');
const app = express();
app.use(express.json());
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
✅ Tip: Always keep your app modular as it grows. We’ll keep things simple for now.
📘 Step 3: Add Mock Data
let books = [
{ id: 1, title: "1984", author: "George Orwell" },
{ id: 2, title: "The Hobbit", author: "J.R.R. Tolkien" },
];
🧪 Step 4: Define API Routes
➤ Get All Books
app.get('/books', (req, res) => {
res.json(books);
});
➤ Get a Single Book by ID
app.get('/books/:id', (req, res) => {
const book = books.find(b => b.id == req.params.id);
if (!book) return res.status(404).send('Book not found');
res.json(book);
});
➤ Add a New Book
app.post('/books', (req, res) => {
const book = {
id: books.length + 1,
title: req.body.title,
author: req.body.author
};
books.push(book);
res.status(201).json(book);
});
➤ Update a Book
app.put('/books/:id', (req, res) => {
const book = books.find(b => b.id == req.params.id);
if (!book) return res.status(404).send('Book not found');
book.title = req.body.title;
book.author = req.body.author;
res.json(book);
});
➤ Delete a Book
app.delete('/books/:id', (req, res) => {
books = books.filter(b => b.id != req.params.id);
res.status(204).send();
});
📊 Chart: Most Used Web Frameworks (Stack Overflow 2023)

Framework | Usage (%) |
---|---|
Express.js | 22% |
FastAPI | 11% |
Spring Boot | 20% |
Flask | 17% |
Express still leads for quick REST API projects.
🛡️ Best Practices to Keep in Mind
- Use a proper error handler middleware.
- Always validate request data (use
Joi
,express-validator
etc.). - Use environment variables (
dotenv
) for config. - Split routes into separate files once app grows.
- Use a database (like MongoDB or PostgreSQL) instead of in-memory data.
🧪 Testing the API
Use Postman to:
GET /books
POST /books
with a JSON bodyPUT /books/:id
DELETE /books/:id
Each response should reflect your changes correctly.

✅ Conclusion
Building a REST API using Node.js and Express is one of the most foundational skills for modern backend developers. It’s not just about writing endpoints—it’s about structuring data flow, understanding HTTP methods, and building scalable logic. As shown, even a basic setup can handle core CRUD operations efficiently.
Keep practicing by connecting a real database or implementing authentication next.
Frequently Asked Questions (FAQs)
1. What is a REST API and why should I use it with Node.js?
A REST API (Representational State Transfer) is an architectural style for designing networked applications. Using Node.js with REST APIs is popular because Node is lightweight, fast, and well-suited for asynchronous tasks—making it ideal for building scalable APIs.
2. Why is Express used in building REST APIs?
Express simplifies the process of building APIs in Node.js by handling routing, request/response handling, middleware, and error management. It’s minimalistic yet powerful and highly customizable.
3. Do I need a database to build a REST API with Node.js and Express?
No, a database is not mandatory for learning or building basic APIs. However, for real-world applications, integrating a database (like MongoDB or PostgreSQL) is essential to persist and manage data beyond runtime memory.
4. What are the key HTTP methods used in a REST API?
The core methods include:
GET
– Fetch dataPOST
– Create new dataPUT
– Update existing dataDELETE
– Remove data
These methods map directly to CRUD operations (Create, Read, Update, Delete).
5. Can I secure my Node.js REST API?
Yes. Common approaches include:
- Using authentication tools like JWT (JSON Web Tokens)
- Implementing HTTPS
- Validating input data
- Rate limiting and CORS management
- Storing secrets using environment variables
6. How do I test a REST API built with Express?
You can use tools like:
- Postman for manual testing
- Jest or Mocha with Supertest for automated testing
These help you simulate HTTP requests and validate API responses.
7. Is Express still relevant in 2025?
Yes. Despite new frameworks emerging, Express remains widely adopted for its simplicity, strong ecosystem, and vast community support. It’s often chosen for learning, prototyping, and even production environments.