Creating REST API with Express.js
Creating REST API with Express.js in Node.js
Creating a REST API with Express.js in Node.js is a core skill in backend development. A REST API allows your application to handle data operations like creating, reading, updating, and deleting data. In this lesson, you will learn how to create a REST API with Express.js step by step using simple examples.
Setup for REST API in Express.js
Install Express.js
npm init -y
npm install express
Basic Server Setup
const express = require(‘express’);
const app = express();
app.use(express.json());
app.listen(3000, () => {
console.log(‘Server running on port 3000’);
});
This setup is required to create a REST API with Express.js in Node.js.
Creating REST API Routes in Express.js
Sample Data
let users = [
{ id: 1, name: “Amit” },
{ id: 2, name: “Rahul” }
];
GET API in Express.js
Get All Users
app.get(‘/users’, (req, res) => {
res.json(users);
});
This GET API returns all users from the data.
GET API by ID
Get Single User
app.get(‘/users/:id’, (req, res) => {
const user = users.find(u => u.id == req.params.id);
res.json(user);
});
This API fetches a specific user using route parameters.
POST API in Express.js
Create New User
app.post(‘/users’, (req, res) => {
const newUser = {
id: users.length + 1,
name: req.body.name
};
users.push(newUser);
res.json(newUser);
});
POST API is used to add new data.
PUT API in Express.js
Update User
app.put(‘/users/:id’, (req, res) => {
const user = users.find(u => u.id == req.params.id);
user.name = req.body.name;
res.json(user);
});
PUT API updates existing data.
DELETE API in Express.js
Delete User
app.delete(‘/users/:id’, (req, res) => {
users = users.filter(u => u.id != req.params.id);
res.send(‘User deleted’);
});
DELETE API removes data from the server.
Testing REST API in Express.js
You can test your REST API using tools like Postman or browser for GET requests. Testing ensures that your API works correctly.
Real-World Use of REST API
Creating REST API with Express.js is used in applications like user management systems, e-commerce platforms, and mobile apps. It is a key skill for backend developers.
Best Practices for REST API in Express.js
Use Proper Status Codes
Use status codes like 200, 201, 404, and 500 for better API design.
Validate Data
Always validate request data before processing.
Use Clean Routes
Keep routes simple and resource-based.
FAQs on Creating REST API with Express.js
How to create REST API in Node.js
You can create REST API using Express.js by defining routes and handling HTTP methods.
What is CRUD in REST API
CRUD stands for Create, Read, Update, and Delete operations.
Is Express.js good for REST API
Yes, Express.js is widely used for building REST APIs in Node.js.
Continue Learning Backend Development
Now that you understand how to create REST API with Express.js, you are ready to move to the next lesson where you will learn about API testing using tools like Postman. To explore more structured tutorials and courses, click here for more free courses.



