---
title: "How do you handle routing in Express.js?"  
description: "How do you handle routing in Express.js?"  
author: "Revati S Misra"  
published: 2023-09-27  
updated: 2023-09-27  
canonical: https://www.mindstick.com/forum/159961/how-do-you-handle-routing-in-express-js  
category: "javascript"  
tags: ["javascript", "node js", "express js"]  
reading_time: 2 minutes  

---

# How do you handle routing in Express.js?

How do you [handle](https://www.mindstick.com/articles/311004/suede-skillet-handle-cover) [routing](https://www.mindstick.com/articles/11998/attribute-routing-in-asp-dot-net-mvc) in Express.js?

## Replies

### Reply by Aryan Kumar

Handling routing in Express.js involves defining routes for different HTTP methods (GET, POST, PUT, DELETE, etc.) and specifying how the server should respond to client requests for each route. Express provides a simple and flexible way to define routes using the **express.Router** object. Here's how to handle routing in an Express.js application:

- **Create an Express Application**
- **Define Routes**
- **Middleware for routes**
- **Start the Express Server**

```plaintext
const express = require('express');
const app = express();
const port = 3000;

// Middleware to log requests
app.use((req, res, next) => {
  console.log(`Request received at ${new Date()}`);
  next();
});

// Handling a GET request to the root URL ("/")
app.get('/', (req, res) => {
  res.send('Hello, World!');
});

// Handling a POST request to "/api/users"
app.post('/api/users', (req, res) => {
  // Handle the POST request here, e.g., create a new user
  res.send('User created successfully');
});

// Handling a PUT request to "/api/users/:id" (with a dynamic parameter)
app.put('/api/users/:id', (req, res) => {
  const userId = req.params.id; // Access dynamic parameter ":id"
  // Handle the PUT request here, e.g., update user information
  res.send(`User with ID ${userId} updated`);
});

// Handling a DELETE request to "/api/users/:id"
app.delete('/api/users/:id', (req, res) => {
  const userId = req.params.id; // Access dynamic parameter ":id"
  // Handle the DELETE request here, e.g., delete a user
  res.send(`User with ID ${userId} deleted`);
});

// Start the Express server
app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});
```

Your Express application is now ready to handle HTTP requests based on the defined routes and route handlers.

Express.js provides powerful routing capabilities, allowing you to build complex APIs and web applications by defining routes for different HTTP methods and handling various types of requests.

This code creates an Express.js application with a few routes (GET, POST, PUT, DELETE) and a request logging middleware. It listens on port 3000 for incoming HTTP requests.

You can run this code by saving it in a JavaScript file (e.g., **app.js**) and executing it using Node.js. Make sure to have Express.js installed (**npm install express**) in your project directory before running the code.


---

Original Source: https://www.mindstick.com/forum/159961/how-do-you-handle-routing-in-express-js

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
