res.redirect helps send users to a different web page automatically. It makes your app move users from one URL to another easily.
res.redirect for redirections in Express
res.redirect([status,] path)
You can give just the path or add a status code like 301 or 302.
Status code 302 means temporary redirect, 301 means permanent redirect.
res.redirect('/home')res.redirect(301, '/new-page')
res.redirect('https://example.com')This Express app sends users from '/old-page' to '/new-page' permanently using res.redirect with status 301.
import express from 'express'; const app = express(); app.get('/', (req, res) => { res.send('Welcome to the homepage!'); }); app.get('/old-page', (req, res) => { res.redirect(301, '/new-page'); }); app.get('/new-page', (req, res) => { res.send('This is the new page.'); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Always set the status code if you want to control if the redirect is permanent or temporary.
Redirects stop the current response and send the new location to the browser.
Use absolute URLs to redirect outside your site, relative paths for internal pages.
res.redirect sends users to a different URL automatically.
You can specify if the redirect is permanent (301) or temporary (302).
It is useful for moving pages, login flows, and guiding users.
Practice
res.redirect('/home') do in an Express app?Solution
Step 1: Understand res.redirect purpose
Theres.redirectmethod sends an HTTP redirect response to the client, telling the browser to go to a new URL.Step 2: Analyze the argument '/home'
Passing '/home' means the user will be redirected to the '/home' path on the same server.Final Answer:
Sends the user to the '/home' URL automatically -> Option CQuick Check:
res.redirect('/home') = Redirect to '/home' [OK]
- Thinking it sends JSON data
- Confusing redirect with stopping server
- Assuming it logs to console
Solution
Step 1: Recall res.redirect syntax for status code
Express usesres.redirect(statusCode, url)to specify redirect type.Step 2: Identify correct order of arguments
The status code (301) must come first, then the URL string.Final Answer:
res.redirect(301, '/dashboard'); -> Option BQuick Check:
Status code first, URL second = res.redirect(301, '/dashboard'); [OK]
- Swapping arguments order
- Using non-existent res.redirectPermanent method
- Passing status code as second argument
app.get('/old', (req, res) => {
res.redirect('/new');
});Solution
Step 1: Understand route handler behavior
The handler for '/old' callsres.redirect('/new'), which sends a redirect response.Step 2: Browser reaction to redirect
The browser receives the redirect and automatically requests the '/new' URL instead of '/old'.Final Answer:
Redirect the browser to '/new' URL -> Option DQuick Check:
res.redirect('/new') = Browser goes to '/new' [OK]
- Expecting original page content to show
- Thinking it returns 404 error
- Confusing redirect with JSON response
app.get('/start', (req, res) => {
res.redirect('/end', 302);
});Solution
Step 1: Check res.redirect argument order
Express expectsres.redirect(statusCode, url), status code first.Step 2: Identify incorrect argument order
The code usesres.redirect('/end', 302), which is reversed and causes unexpected behavior.Final Answer:
The status code should be first argument, not second -> Option AQuick Check:
Correct order: status, URL [OK]
- Swapping status and URL arguments
- Thinking res.redirect can't be used in routes
- Believing relative URLs are invalid
- Assuming 302 is invalid status
res.redirect to do this?Solution
Step 1: Check login condition usage
The code must check ifreq.userexists to confirm login.Step 2: Redirect only if logged in
If logged in, redirect to '/dashboard'; otherwise, send a message to login.Step 3: Verify correct use of res.redirect and res.send
if (req.user) { res.redirect('/dashboard'); } else { res.send('Please login'); } correctly callsres.redirectinside the if block and sends response otherwise, avoiding multiple responses.Final Answer:
if (req.user) { res.redirect('/dashboard'); } else { res.send('Please login'); } -> Option AQuick Check:
Redirect only when logged in = if (req.user) { res.redirect('/dashboard'); } else { res.send('Please login'); } [OK]
- Calling res.redirect without condition
- Sending multiple responses in one request
- Redirecting when user is not logged in
Ready to prove it?
Pass the test and this pattern moves unknown
