Express res.redirect() Method

Beginner
⏱️ 8 min read
📚 Updated: May 2026
🎯 3 Code Examples

What you’ll learn

  • How to redirect requests with res.redirect().
  • How to choose appropriate redirect status codes.
  • How to use redirects for auth and route migration flows.
  • How to avoid redirect-related response bugs.

Syntax

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

Default temporary redirect (302)

javascript
app.get('/old-dashboard', function (req, res) {
  res.redirect('/dashboard');
});
2

Permanent redirect (301) after route change

javascript
app.get('/docs-v1', function (req, res) {
  res.redirect(301, '/docs');
});
3

Redirect unauthenticated user to login

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

⚠️ Common pitfalls

  • Return immediately after res.redirect() to avoid extra response writes.
  • Use 301 only when target is truly permanent.
  • Avoid redirect loops by checking route conditions carefully.

❓ FAQ

It sends an HTTP redirect response by setting Location and an appropriate 3xx status code.
By default Express uses 302 (Found) if no status code is specified.
Yes, pass status first, for example res.redirect(301, '/new-url').
Both work, but use clear and trusted targets; absolute URLs are often better for external redirects.
Continuing handler logic after redirecting, which can cause accidental double responses.
Did you know?

res.redirect() sets a redirect status and Location header, then ends the response.

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.

4 people found this page helpful