Performance: res.redirect for redirections
This affects page load speed by causing an additional HTTP request and response cycle before the final content loads.
app.get('/old-page', (req, res) => { res.status(200).sendFile(__dirname + '/path/to/new-page.html'); });
app.get('/old-page', (req, res) => { res.redirect('/new-page'); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| res.redirect causing extra HTTP request | N/A | N/A | Blocks initial paint until redirect completes | [X] Bad |
| Directly serving final content without redirect | N/A | N/A | Paints content on first response | [OK] Good |
res.redirect('/home') do in an Express app?res.redirect method sends an HTTP redirect response to the client, telling the browser to go to a new URL.res.redirect(statusCode, url) to specify redirect type.app.get('/old', (req, res) => {
res.redirect('/new');
});res.redirect('/new'), which sends a redirect response.app.get('/start', (req, res) => {
res.redirect('/end', 302);
});res.redirect(statusCode, url), status code first.res.redirect('/end', 302), which is reversed and causes unexpected behavior.res.redirect to do this?req.user exists to confirm login.res.redirect inside the if block and sends response otherwise, avoiding multiple responses.Pass the test and this pattern moves unknown