Performance: res.status for status codes
This affects server response time and perceived page load speed by setting HTTP status codes efficiently.
res.status(404).send('Not Found');
res.send('Not Found'); // without setting status code explicitly| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| res.send() without status | 0 (server-side) | 0 | 0 | [X] Bad |
| res.status(code).send() | 0 (server-side) | 0 | 0 | [OK] Good |
res.status(404) do in an Express route handler?res.status(code) sets the HTTP status code for the response but does not send the response itself.res.status(code) sets the status code, then res.json(data) sends the JSON response.res.status(201) with res.json({ message: 'Created' }). Others misuse method order or parameters.app.get('/test', (req, res) => {
res.status(500);
res.send('Server error');
});res.status(500) sets the HTTP status code to 500. Then res.send('Server error') sends the response body with that status.app.get('/error', (req, res) => {
res.send('Oops!');
res.status(400);
});res.send() is called, the response is sent and closed. Any code after it won't affect the response.res.send('Oops!') sends the response with default status 200. Then res.status(400) is called but ignored because response is already sent.res.status(204).send() sends status 204 with empty body [check].res.status(204).json(null) sends status 204 with JSON "null" body ✗.res.sendStatus(204) sends status 204 with "No Content" text body ✗.Pass the test and this pattern moves unknown