Logo

Developer learning path

Node.js

Routing in Express.js in Node.js

Routing in Express.js

55

#description

Express.js is a popular web framework for Node.js that makes it easy to build web applications and APIs. One of the key features of Express is its ability to handle routing, which is the process of matching a requested URL to a specific handler function.

Routing in Express involves defining a set of URL patterns and associating each pattern with a callback function that handles the request. For example, if we have a route defined for the path /users, any requests to that path will be directed to the appropriate route handler function.

To define a route in Express, we use the app object returned by the express() function.

For example:

                    
const express = require('express');
const app = express();

// Define a route for GET requests to /users
app.get('/users', function(req, res) {
  res.send('List of users');
});
                  

In this example, we're defining a route for GET requests to the /users path. The second argument is a callback function that is called when a request is made to that path. In this case, we're simply sending back the response "List of users".

We can also use route parameters to capture dynamic values from the URL.

For example:

                    
// Define a route with a parameter for user IDs
app.get('/users/:id', function(req, res) {
  res.send('Details for user ' + req.params.id);
});
                  

In this example, we're defining a route that takes a parameter :id in the URL. The req.params.id property contains the value of the id parameter from the URL.

Routes can also be organized into groups using the Router class. This allows us to define a middleware stack for a group of related routes.

For example:

                    
// Define a router for the /users path
const usersRouter = express.Router();

// Define middleware for all requests to the /users path
usersRouter.use(function(req, res, next) {
  console.log('Request received for /users');
  next();
});

// Define a route for GET requests to /users
usersRouter.get('/', function(req, res) {
  res.send('List of users');
});

// Define a nested router for the /users/:id path
const userRouter = express.Router({ mergeParams: true });

// Define a middleware for all requests to the /users/:id path
userRouter.use(function(req, res, next) {
  console.log('Request received for user ID ' + req.params.id);
  next();
});

// Define a route for GET requests to /users/:id
userRouter.get('/', function(req, res) {
  res.send('Details for user ' + req.params.id);
});

// Mount the routers on the main app
app.use('/users', usersRouter);
app.use('/users/:id', userRouter);
                  

In this example, we define two routers: one for the /users path and one nested inside it for the /users/:id path. We use middleware to log incoming requests for each path. Finally, we mount the two routers on the main app using the use() method.

March 25, 2023

If you don't quite understand a paragraph in the lecture, just click on it and you can ask questions about it.

If you don't understand the whole question, click on the buttons below to get a new version of the explanation, practical examples, or to critique the question itself.