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?
app.get('/old-page', (req, res) => { res.redirect('/new-page'); });
Think about how relative URLs starting with '/' behave in web browsers.
When res.redirect is called with a URL starting with '/', it tells the browser to go to that path on the current domain. So the browser changes the URL to '/new-page' and loads that page.
Which of the following Express code snippets correctly uses res.redirect to redirect to 'https://example.com'?
Check the official Express documentation for res.redirect method signatures.
The correct syntax to specify a status code and URL is res.redirect(statusCode, url). The status code must be a number, not a string. Option C uses 301 as a number, so it is correct.
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?
app.get('/start', (req, res) => { res.redirect('/middle'); res.send('Done'); });
Think about what happens when you try to send two responses for one request.
Once res.redirect sends a response, calling res.send again causes an error because the response is already finished.
Given this Express code:
app.get('/go', (req, res) => {
res.redirect('/target');
});What HTTP status code will the client receive?
app.get('/go', (req, res) => { res.redirect('/target'); });
Check the Express documentation for the default redirect status code.
By default, res.redirect sends a 302 status code, which means the resource is temporarily found at a new location.
If you use res.redirect('https://otherdomain.com/page') in Express, what happens in the browser?
Think about how browsers handle full URLs in HTTP redirects.
When res.redirect is given a full URL with a different domain, it sends a redirect response with that URL. The browser then navigates to that external domain.