0
0
Expressframework~20 mins

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

Choose your learning style9 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.