Performance: Status code usage patterns
This affects server response time and client rendering speed by controlling how quickly browsers and clients understand the result of a request.
res.status(404).send({ error: 'User not found' });
res.status(200).send({ error: 'User not found' });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Incorrect status code (e.g., 200 on error) | No direct DOM impact | No reflows | No paint cost | [X] Bad |
| Correct status code (e.g., 404, 500) | No direct DOM impact | No reflows | No paint cost | [OK] Good |
| Meta refresh redirect | No direct DOM impact | Blocks rendering until meta processed | Delays paint | [X] Bad |
| HTTP redirect status (301/302) | No direct DOM impact | No reflows | No paint cost | [OK] Good |
200 generally indicate in a Node.js response?res.status(code) to set status codes before sending a response.res.status(404).send('Not Found'); correctly sets status 404 and sends the message.app.get('/data', (req, res) => {
if (!req.query.id) {
return res.status(400).send('Missing id');
}
res.status(200).send('Data for ' + req.query.id);
});/data without an id query parameter?req.query.id is missing and returns status 400 with message 'Missing id'.id is absent, the server responds immediately with status 400 (bad request).app.post('/submit', (req, res) => {
if (!req.body.name) {
res.sendStatus(422);
}
res.status(201).send('Created');
});!req.body.name is true, res.sendStatus(422) sends a response but code continues.res.status(201).send('Created'), causing an error because response was already sent.name is missing, causing an error. -> Option Dreturn after sending 403 JSON error, preventing further code execution.Pass the test and this pattern moves unknown