Performance: Error response formatting
This affects server response time and client rendering speed by controlling payload size and parsing complexity.
res.status(500).json({ error: 'Database failure', code: 'DB_ERR' });
res.status(500).json({ error: err.message, stack: err.stack, data: largeDataObject });| Pattern | Payload Size | Parsing Time | Client Render Delay | Verdict |
|---|---|---|---|---|
| Full error object with stack and data | Large (20-50kb) | High | High (50-100ms delay) | [X] Bad |
| Minimal error message with code | Small (<2kb) | Low | Low (<10ms delay) | [OK] Good |
res.status() in Node.js error handling?res.status()res.status(code) to set status, then json() to send JSON.res.status(404).json({ error: 'Not found' }) is valid syntax.app.use((err, req, res, next) => {
res.status(500).json({
message: err.message,
code: 'SERVER_ERROR'
});
});app.use((err, req, res) => {
res.status(400).json({ message: err.message });
});next, so Express won't recognize it as error handler.statusCode property. Otherwise, send a 500 status with a generic message. Which code snippet correctly implements this in Express?err.statusCode if present, else default to 500; similarly for message.res.status(status) and send JSON with code and message fields.Pass the test and this pattern moves unknown