Performance: res.json for JSON responses
This affects server response time and client rendering speed by efficiently sending JSON data.
res.json(data);
res.setHeader('Content-Type', 'application/json'); res.send(JSON.stringify(data));
| Pattern | Server Processing | Response Size | Client Parsing | Verdict |
|---|---|---|---|---|
| Manual JSON.stringify + setHeader | Extra stringify step | Same size | Standard parsing | [!] OK |
| res.json method | Built-in optimized stringify | Same size | Standard parsing | [OK] Good |
res.json() do in an Express app?res.json()res.json(), not res.sendJson() or res.jsonify().{ message: 'Hello' }.app.get('/data', (req, res) => {
res.json({ success: true, count: 5 });
});app.get('/info', (req, res) => {
res.json = { name: 'Alice' };
});res.json to do this?const users = [
{ id: 1, active: true },
{ id: 2, active: false },
{ id: 3, active: true }
];
app.get('/active-users', (req, res) => {
// Fill in here
});users.filter(user => user.active) to get only users where active is true.res.json() to send it properly formatted.Pass the test and this pattern moves unknown