Express.js res.redirect() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Response

Definition

res.redirect() tells the client to go to another URL. Express sets a redirect status (default 302 Found), sets the Location header, and ends the response.

Use it for moved routes, login gates, and post/redirect/get flows. To set Location without finishing the response, see res.location(). Status-only helpers live on res.status().

Syntax

Call res.redirect in your Express.js app. Start with this line.

Express
res.redirect(path)
res.redirect(status, path)
CallWhat it does
res.redirect(path)Primary signature.
res.redirect(status, path)Related call form.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

res.redirect(path)
res.redirect(status, path)

Parameters

ParameterDescription
statusOptional. Optional HTTP redirect code. Default 302. Common: 301 (permanent), 302/303/307/308.
pathRequired. Where to send the client: absolute URL, root-relative (/admin), or relative (../login).
AbsoluteRequired. Other host
Root-relativeRequired. Same host, from site root
RelativeRequired. Resolved by the browser from the current URL

Return Value

res.redirect() sets a 3xx status (default 302), sets Location, and ends the response. Prefer return res.redirect(...) so later code does not run.

Express
return res.redirect('/login');
return res.redirect(301, 'https://example.com');

Express.js Examples

Example 1 — Default Temporary Redirect

Send the client to another path with the default 302.

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

app.get('/old-dashboard', (req, res) => {
  res.redirect('/dashboard');
});

app.get('/dashboard', (req, res) => {
  res.send('Dashboard');
});

app.listen(3000);

How It Works

  1. Client hits the old path. /old-dashboard matches the first route.
  2. Redirect with one argument. Status defaults to 302.
  3. Location is set. Browser receives /dashboard.
  4. Response ends. Client then requests the new URL.

Example 2 — Permanent Redirect (301)

Use status-first syntax (Express 5) when a URL has moved for good.

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

app.get('/docs-v1', (req, res) => {
  // Express 5: status, then URL
  res.redirect(301, '/docs');
});

app.get('/docs', (req, res) => {
  res.send('Docs home');
});

app.listen(3000);

How It Works

  1. Pass status first. res.redirect(301, '/docs') is the Express 5 form.
  2. 301 means permanent. Clients and caches may keep the new URL.
  3. Do not reverse the args. res.redirect('/docs', 301) is removed in Express 5.
  4. Use 301 only when sure. Temporary moves should stay on 302/307.

Example 3 — Redirect Unauthenticated Users

Gate a page and return after redirect so nothing else runs.

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

app.get('/account', (req, res) => {
  if (!req.user) {
    return res.redirect(302, '/login');
  }
  res.send('Welcome to your account');
});

app.get('/login', (req, res) => {
  res.send('Please log in');
});

app.listen(3000);

How It Works

  1. Check auth first. Missing user → redirect path.
  2. return after redirect. Stops a second res.send (double-response bug).
  3. 302 keeps it temporary. Login gates are not permanent moves.
  4. Logged-in users continue. The welcome body is sent only when allowed.

Runtime Support

res.redirect() is an Express.js response method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only follow the 3xx + Location they receive.

✓ Server · Express 4 & 5

res.redirect()

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.

Server Node.js runtime API
Node.js 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
res.redirect() Express on Node.js

Bottom line: Study and run these examples with Node.js and the express package. Copy a snippet into a local file and start the server with node.

Frequently Asked Questions

It sends an HTTP redirect: sets a 3xx status (default 302), sets the Location header, and ends the response so the client follows the new URL.
302 Found. Pass a status first when you need another code, for example res.redirect(301, '/docs').
Use res.redirect(status, url) only — not res.redirect(url, status). The magic string 'back' was removed; use req.get('Referrer') || '/' instead.
res.location() only sets the Location header. res.redirect() sets Location, chooses a redirect status, and finishes the response.
No. It runs on your Express server. The browser receives the 3xx response and Location header, then requests the new URL.

Next: res.render()

Render a view template and send the HTML to the client.

res.render() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful