codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🚂

Express.js

20 / 76 topics
20Redirecting Requests in Express.js21Setting HTTP Status Codes22Sending JSON Responses
Tutorials/Express.js/Redirecting Requests in Express.js
🚂Express.js

Redirecting Requests in Express.js

Updated 2026-05-15
10 min read

Redirecting Requests in Express.js

Introduction

In web development, redirection is a common task. It involves sending the client to a different URL than the one they originally requested. This can be useful for various reasons such as handling legacy URLs, directing users to a new page after an action, or managing authentication.

Express.js provides several methods to handle redirections, making it straightforward to implement these functionalities in your applications.

Concept

In Express.js, you can redirect a request using the res.redirect() method. This method takes one argument: the URL to which you want to redirect the client. You can also specify an optional status code as the first argument, with the default being 302 (Found).

The res.redirect() method ends the response process and sends a redirect HTTP header to the client.

Examples

Basic Redirection

Here's a simple example of how to redirect a request to another route within your Express application:

JavaScript
1const express = require('express');
2const app = express();
3
4app.get('/old-route', (req, res) => {
5res.redirect('/new-route');
6});
7
8app.get('/new-route', (req, res) => {
9res.send('Welcome to the new route!');
10});
11
12app.listen(3000, () => {
13console.log('Server is running on http://localhost:3000');
14});

In this example, any request made to /old-route will be redirected to /new-route.

Redirect with Status Code

You can also specify a status code when redirecting. For instance, if you want to use a permanent redirect (HTTP 301), you can do so like this:

JavaScript
1app.get('/old-page', (req, res) => {
2res.redirect(301, '/new-page');
3});
4
5app.get('/new-page', (req, res) => {
6res.send('This is the new page.');
7});

Redirecting to External URLs

You can also redirect users to external websites:

JavaScript
1app.get('/external-link', (req, res) => {
2res.redirect('https://www.example.com');
3});

In this case, any request made to /external-link will be redirected to https://www.example.com.

Redirecting Based on Conditions

You can use conditions to decide whether to redirect a request. For example, you might want to redirect users based on their authentication status:

JavaScript
1app.get('/protected-page', (req, res) => {
2if (!req.isAuthenticated()) {
3 return res.redirect('/login');
4}
5res.send('Welcome to the protected page!');
6});

In this example, if a user is not authenticated, they will be redirected to the /login route.

What's Next?

After learning how to redirect requests in Express.js, you might want to explore setting HTTP status codes. Understanding and using different status codes can help you manage responses more effectively and provide better feedback to clients.

By mastering redirections and response handling in Express.js, you'll be well-equipped to build robust and user-friendly web applications.


PreviousRoute Parameters in Express.jsNext Setting HTTP Status Codes

Recommended Gear

Route Parameters in Express.jsSetting HTTP Status Codes