Routing is one of the most important concepts in Express.js. Every web application and REST API relies on routes to determine how requests are handled. Understanding GET, POST, PUT, and DELETE requests is essential for building modern web applications and APIs with Node.js.
If you're new to Express.js, start with our guide: Express.js Tutorial for Beginners: Build Your First Web Server
What Is Routing in Express.js?
Routing determines how an application responds to requests made to specific URLs and HTTP methods. Each route contains a URL path and a callback function that executes when a request matches the route.
app.get('/', (req, res) => {
res.send('Welcome');
});
In this example, the route responds when users visit the homepage.
Understanding HTTP Methods
| Method | Purpose | CRUD Operation |
|---|---|---|
| GET | Retrieve data | Read |
| POST | Create data | Create |
| PUT | Update data | Update |
| DELETE | Delete data | Delete |
GET Request Example
GET requests retrieve information from a server.
app.get('/products', (req, res) => {
res.send('All Products');
});
Visiting:
http://localhost:3000/products
Returns:
All Products
GET Request Returning JSON
app.get('/api/products', (req, res) => {
res.json([
{
id: 1,
name: 'Laptop'
},
{
id: 2,
name: 'Mouse'
}
]);
});
This is commonly used in REST APIs.
POST Request Example
POST requests create new resources.
Enable JSON support:
app.use(express.json());
Create a route:
app.post('/products', (req, res) => {
const product = req.body;
res.json({
message: 'Product Created',
data: product
});
});
Example request:
{
"name": "Keyboard",
"price": 25
}
PUT Request Example
PUT requests update existing resources.
app.put('/products/:id', (req, res) => {
res.json({
message: 'Product Updated',
productId: req.params.id,
data: req.body
});
});
Example:
PUT /products/5
Updates product ID 5.
DELETE Request Example
DELETE requests remove resources.
app.delete('/products/:id', (req, res) => {
res.json({
message: 'Product Deleted',
productId: req.params.id
});
});
Example:
DELETE /products/5
Route Parameters
Route parameters allow dynamic values in URLs.
app.get('/users/:id', (req, res) => {
res.send(`User ID: ${req.params.id}`);
});
Request:
/users/25
Output:
User ID: 25
Multiple Route Parameters
app.get('/users/:userId/orders/:orderId', (req, res) => {
res.json(req.params);
});
Request:
/users/10/orders/100
Query Parameters
Query parameters are commonly used for searching and filtering.
app.get('/search', (req, res) => {
res.send(req.query.keyword);
});
Example:
/search?keyword=nodejs
Output:
nodejs
Using Express Router
For large projects, routes should be separated into modules.
Create products.js
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
res.send('Products List');
});
module.exports = router;
Load Router
const productRoutes = require('./routes/products');
app.use('/products', productRoutes);
This improves project organization significantly.
Route Middleware Example
function auth(req, res, next) {
console.log('Authenticated');
next();
}
app.get('/dashboard', auth, (req, res) => {
res.send('Dashboard');
});
Middleware executes before the route handler.
Common Routing Mistakes
REST API Best Practices
- Use nouns in URLs
- Use proper HTTP methods
- Return meaningful status codes
- Validate request data
- Handle errors properly
- Use middleware for authentication
- Keep routes organized using Router
Conclusion
Routing is the backbone of every Express.js application. By understanding GET, POST, PUT, and DELETE requests, you'll be able to build powerful REST APIs and scalable web applications.
Mastering Express.js routing is a major step toward becoming a professional Node.js developer and building enterprise-grade applications.
About ShasTech-IT
ShasTech-IT provides Node.js development, web application development, Android apps, ERP systems, HRMS solutions, WMS platforms, POS software, and custom business applications tailored to organizational needs.