Skilled in SEO, content writing, and digital marketing. Completed several years of working in many organizations including multinational companies.
I love to learn new things in life that keep me motivated.
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
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.
Liked By
Write Answer
How do you handle routing in Express.js?
Join MindStick Community
You have need login or register for voting of answers or question.
Aryan Kumar
27-Sep-2023Handling 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:
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.