Express.js Routing Explained: GET, POST, PUT & DELETE Requests with Examples

Learn Express.js routing with practical GET, POST, PUT, and DELETE examples. Understand REST APIs, route parameters, request handling, and best practices for Node.js development.

What we do
  • WordPress Themes & Plugins
  • E-commerce, News Portals, Web Apps
  • Mobile & Desktop Applications
  • Professional IT Training Center
Node.js Development
Published: 2026-06-16 17:04:03

Article

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.

What You'll Learn: Express.js routing fundamentals, HTTP methods, route parameters, query strings, request handling, REST API design, and practical examples using GET, POST, PUT, and DELETE routes.

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
These four methods form the foundation of most REST APIs.

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

Check route spelling, URL path, and HTTP method.

Ensure express.json() middleware is enabled.

Ensure you're using GET, POST, PUT, or DELETE correctly.
Encountering errors? Read: Common Node.js Errors and How to Fix Them

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.

Contact WhatsApp