Bird
Raised Fist0
Expressframework~20 mins

res.redirect for redirections in Express - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redirect Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when res.redirect is called with a relative URL?

Consider this Express route handler:

app.get('/old-page', (req, res) => {
  res.redirect('/new-page');
});

What will the browser do when a user visits /old-page?

Express
app.get('/old-page', (req, res) => {
  res.redirect('/new-page');
});
AThe browser will navigate to 'http://new-page' as a full URL.
BThe browser will stay on '/old-page' and display the content of '/new-page' without changing the URL.
CThe browser will throw an error because the URL is relative.
DThe browser will navigate to the absolute path '/new-page' on the same domain.
Attempts:
2 left
💡 Hint

Think about how relative URLs starting with '/' behave in web browsers.

📝 Syntax
intermediate
2:00remaining
Which res.redirect usage is syntactically correct?

Which of the following Express code snippets correctly uses res.redirect to redirect to 'https://example.com'?

Ares.redirect('https://example.com');
Bres.redirect('301', 'https://example.com');
Cres.redirect(301, 'https://example.com');
Dres.redirect(redirectUrl = 'https://example.com');
Attempts:
2 left
💡 Hint

Check the official Express documentation for res.redirect method signatures.

🔧 Debug
advanced
2:00remaining
Why does this code throw a server error?

Look at this Express route:

app.get('/start', (req, res) => {
  res.redirect('/middle');
  res.send('Done');
});

Why does this code cause a server-side error?

Express
app.get('/start', (req, res) => {
  res.redirect('/middle');
  res.send('Done');
});
ACalling res.send after res.redirect causes an error because the response is already sent.
Bres.redirect does not send a response, so res.send is needed to complete it.
CExpress requires next() to be called after res.redirect to complete the redirect.
DThe redirect URL '/middle' is invalid and causes the redirect to fail silently.
Attempts:
2 left
💡 Hint

Think about what happens when you try to send two responses for one request.

❓ state_output
advanced
2:00remaining
What is the HTTP status code sent by default with res.redirect?

Given this Express code:

app.get('/go', (req, res) => {
  res.redirect('/target');
});

What HTTP status code will the client receive?

Express
app.get('/go', (req, res) => {
  res.redirect('/target');
});
A302 (Found)
B308 (Permanent Redirect)
C307 (Temporary Redirect)
D301 (Moved Permanently)
Attempts:
2 left
💡 Hint

Check the Express documentation for the default redirect status code.

🧠 Conceptual
expert
3:00remaining
How does res.redirect handle absolute URLs with different domains?

If you use res.redirect('https://otherdomain.com/page') in Express, what happens in the browser?

AExpress blocks redirects to external domains for security and throws an error.
BThe browser navigates to the full URL 'https://otherdomain.com/page' on a different domain.
CThe browser treats it as a relative path and navigates to '/https://otherdomain.com/page' on the current domain.
DThe redirect is ignored and the original page remains loaded.
Attempts:
2 left
💡 Hint

Think about how browsers handle full URLs in HTTP redirects.

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