Discover how one simple command can save you from tricky, error-prone redirection headaches!
Why res.redirect for redirections in Express? - Purpose & Use Cases
Imagine you build a website where users must log in before accessing their profile. Without automatic redirection, you have to manually check if they are logged in and then tell them exactly where to go next.
Manually sending HTTP status codes and setting headers for redirection is tricky and easy to mess up. You might forget to set the right status or URL, causing broken navigation and a bad user experience.
The res.redirect method in Express handles all the details for you. It sends the correct HTTP status and location header so the browser automatically moves the user to the right page smoothly.
res.status(302).set('Location', '/login').end();
res.redirect('/login');It makes sending users to different pages easy, reliable, and clean, improving your app's flow and user experience.
When a user tries to access a protected page without logging in, your app can instantly redirect them to the login page using res.redirect, so they don't get stuck or confused.
Manual redirection requires careful header and status management.
res.redirect simplifies redirection with one clear command.
This improves code clarity and user navigation flow.
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
