Bird
Raised Fist0
Expressframework~5 mins

res.redirect for redirections in Express

Choose your learning style10 modes available
Introduction

res.redirect helps send users to a different web page automatically. It makes your app move users from one URL to another easily.

After a user logs in, send them to their dashboard page.
When a page is moved, send visitors to the new page URL.
If a user tries to access a page they shouldn't, redirect them to the home page.
After a form is submitted, redirect to a thank you page.
To guide users from an old URL to a new one during website updates.
Syntax
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.

Examples
Redirects user to '/home' with default status 302 (temporary).
Express
res.redirect('/home')
Redirects user permanently to '/new-page' with status 301.
Express
res.redirect(301, '/new-page')
Redirects user to an external website URL.
Express
res.redirect('https://example.com')
Sample Program

This Express app sends users from '/old-page' to '/new-page' permanently using res.redirect with status 301.

Express
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');
});
OutputSuccess
Important Notes

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.

Summary

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

(1/5)
1. What does res.redirect('/home') do in an Express app?
easy
A. Sends a JSON response with '/home' data
B. Stops the server from running
C. Sends the user to the '/home' URL automatically
D. Logs '/home' to the console

Solution

  1. Step 1: Understand res.redirect purpose

    The res.redirect method sends an HTTP redirect response to the client, telling the browser to go to a new URL.
  2. Step 2: Analyze the argument '/home'

    Passing '/home' means the user will be redirected to the '/home' path on the same server.
  3. Final Answer:

    Sends the user to the '/home' URL automatically -> Option C
  4. Quick Check:

    res.redirect('/home') = Redirect to '/home' [OK]
Hint: res.redirect sends user to given URL automatically [OK]
Common Mistakes:
  • Thinking it sends JSON data
  • Confusing redirect with stopping server
  • Assuming it logs to console
2. Which is the correct syntax to redirect permanently to '/dashboard' in Express?
easy
A. res.redirect('/dashboard', 301);
B. res.redirect(301, '/dashboard');
C. res.redirectPermanent('/dashboard');
D. res.redirectPermanent(301, '/dashboard');

Solution

  1. Step 1: Recall res.redirect syntax for status code

    Express uses res.redirect(statusCode, url) to specify redirect type.
  2. Step 2: Identify correct order of arguments

    The status code (301) must come first, then the URL string.
  3. Final Answer:

    res.redirect(301, '/dashboard'); -> Option B
  4. Quick Check:

    Status code first, URL second = res.redirect(301, '/dashboard'); [OK]
Hint: Status code goes first in res.redirect(status, url) [OK]
Common Mistakes:
  • Swapping arguments order
  • Using non-existent res.redirectPermanent method
  • Passing status code as second argument
3. What will the browser do after this Express code runs?
app.get('/old', (req, res) => {
  res.redirect('/new');
});
medium
A. Show the content of '/old' page
B. Send a JSON response with '/new' data
C. Return a 404 error
D. Redirect the browser to '/new' URL

Solution

  1. Step 1: Understand route handler behavior

    The handler for '/old' calls res.redirect('/new'), which sends a redirect response.
  2. Step 2: Browser reaction to redirect

    The browser receives the redirect and automatically requests the '/new' URL instead of '/old'.
  3. Final Answer:

    Redirect the browser to '/new' URL -> Option D
  4. Quick Check:

    res.redirect('/new') = Browser goes to '/new' [OK]
Hint: res.redirect sends browser to new URL automatically [OK]
Common Mistakes:
  • Expecting original page content to show
  • Thinking it returns 404 error
  • Confusing redirect with JSON response
4. Find the error in this Express code snippet:
app.get('/start', (req, res) => {
  res.redirect('/end', 302);
});
medium
A. The status code should be first argument, not second
B. res.redirect cannot be used inside app.get
C. The URL must be absolute, not relative
D. 302 is not a valid redirect status code

Solution

  1. Step 1: Check res.redirect argument order

    Express expects res.redirect(statusCode, url), status code first.
  2. Step 2: Identify incorrect argument order

    The code uses res.redirect('/end', 302), which is reversed and causes unexpected behavior.
  3. Final Answer:

    The status code should be first argument, not second -> Option A
  4. Quick Check:

    Correct order: status, URL [OK]
Hint: Status code must come before URL in res.redirect [OK]
Common Mistakes:
  • Swapping status and URL arguments
  • Thinking res.redirect can't be used in routes
  • Believing relative URLs are invalid
  • Assuming 302 is invalid status
5. You want to redirect users from '/login' to '/dashboard' only if they are logged in. Which code correctly uses res.redirect to do this?
hard
A. if (req.user) { res.redirect('/dashboard'); } else { res.send('Please login'); }
B. res.redirect('/dashboard'); if (req.user) { res.send('Welcome'); }
C. res.redirect(302, '/dashboard'); res.send('Logged in');
D. if (!req.user) { res.redirect('/dashboard'); } else { res.send('Please login'); }

Solution

  1. Step 1: Check login condition usage

    The code must check if req.user exists to confirm login.
  2. Step 2: Redirect only if logged in

    If logged in, redirect to '/dashboard'; otherwise, send a message to login.
  3. Step 3: Verify correct use of res.redirect and res.send

    if (req.user) { res.redirect('/dashboard'); } else { res.send('Please login'); } correctly calls res.redirect inside the if block and sends response otherwise, avoiding multiple responses.
  4. Final Answer:

    if (req.user) { res.redirect('/dashboard'); } else { res.send('Please login'); } -> Option A
  5. Quick Check:

    Redirect only when logged in = if (req.user) { res.redirect('/dashboard'); } else { res.send('Please login'); } [OK]
Hint: Redirect inside if block after login check [OK]
Common Mistakes:
  • Calling res.redirect without condition
  • Sending multiple responses in one request
  • Redirecting when user is not logged in
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard