Performance: Response methods and status codes
This concept affects how quickly the server responds and how efficiently the browser processes the response, impacting page load speed and user interaction feedback.
Jump into concepts and practice - no test required
res.status(200).json(data);res.writeHead(200, {'Content-Type': 'application/json'}); res.write(JSON.stringify(data)); res.end();
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual header and body writing | N/A (server-side) | N/A | N/A | [X] Bad |
| Using res.status().json() or res.redirect() | N/A (server-side) | N/A | N/A | [OK] Good |
| Manual redirect with headers | N/A (server-side) | N/A | N/A | [!] OK |
| Using res.status().send() for text | N/A (server-side) | N/A | N/A | [OK] Good |
res.status(404) method do in a Node.js response?res.status()res.status(code) to set status, then call json() to send JSON data.status(200) and json(). Others misuse method order or names.app.get('/test', (req, res) => {
res.status(201).send('Created');
});res.status(201) sets status code 201 (Created), then send('Created') sends that text as the response body.app.get('/redirect', (req, res) => {
res.send('Redirecting...');
res.redirect('/new-url');
});res.send() is called, the response is sent and closed.res.redirect() after res.send() tries to send headers again, causing an error.res.status(400) to set the HTTP status to 400 (Bad Request).json() to send the JSON object with the error message.