Express res.redirect() Method
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.
4 people found this page helpful
