Back to Tutorials
Node.js
35 min read
Sahasransu Satpathy
3/20/2026
Create REST APIs Using Node.js
Step-by-step guide to building RESTful APIs with Node.js and Express.js
Introduction
REST APIs allow communication between frontend and backend using HTTP methods. This tutorial teaches you how to create, read, update, and delete (CRUD) resources using Node.js and Express.js.
Step 1: Project Setup
Initialize your project:
mkdir node-rest-api
cd node-rest-api
npm init -y
npm install express
Step 2: Basic Server
Create index.js:
const express = require('express');
const app = express();
const PORT = 3000;
app.use(express.json());
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});
Step 3: Create a Sample Resource
Let's manage a list of books:
let books = [
{ id: 1, title: 'Node.js Basics', author: 'Sahasransu Satpathy' }
];
Step 4: REST API Routes
GET all books
app.get('/books', (req, res) => {
res.json(books);
});
GET book by ID
app.get('/books/:id', (req, res) => {
const book = books.find(b => b.id == req.params.id);
if(book) res.json(book);
else res.status(404).send('Book not found');
});
POST new book
app.post('/books', (req, res) => {
const book = { id: books.length + 1, ...req.body };
books.push(book);
res.status(201).json(book);
});
PUT update book
app.put('/books/:id', (req, res) => {
const index = books.findIndex(b => b.id == req.params.id);
if(index !== -1) {
books[index] = { id: books[index].id, ...req.body };
res.json(books[index]);
} else res.status(404).send('Book not found');
});
DELETE a book
app.delete('/books/:id', (req, res) => {
books = books.filter(b => b.id != req.params.id);
res.status(204).send();
});
Step 5: Testing the API
- Use Postman, Insomnia, or curl to test CRUD operations.
- Verify GET, POST, PUT, DELETE requests work as expected.
Step 6: Next Steps
- Connect API with a database (MongoDB, PostgreSQL)
- Add validation and error handling
- Implement authentication and authorization
Conclusion
You now have a fully functional REST API using Node.js and Express.js. This forms the foundation for building scalable backend applications and connecting with frontend frameworks.
SEO Suggestions:
- Main keywords: Node.js REST API tutorial, Express API guide, CRUD Node.js, backend API JavaScript
- Meta description: Learn how to build RESTful APIs with Node.js and Express.js. Step-by-step tutorial for beginners with CRUD examples.
- Catchy title suggestions: "Create REST APIs Using Node.js – Step-by-Step Guide", "Node.js REST API Tutorial for Beginners"
Previous Tutorial
Browse All TutorialsNext Tutorial
Browse All Tutorials