Beginner LevelExpress JS
How do you chain HTTP status codes with responses using res.status(404).json()?
❓ Interview Prompt: Explain fluent method chaining with res.status(), setting HTTP response status integers, and returning response payloads.
'res.status(code)' sets the HTTP response status integer (e.g. 200 OK, 201 Created, 400 Bad Request, 404 Not Found, 500 Internal Server Error) on the response header.
Method Chaining:
'res.status()' returns the 'res' object instance itself, allowing fluent chaining directly into '.json()' or '.send()':
'res.status(201).json({ success: true, data: newResource });'.
Code Implementation Example
javascript
import express from 'express';
const app = express();
app.post('/api/items', (req, res) => {
// Return 201 Created status code for newly created resources
res.status(201).json({ id: 101, status: 'created' });
});
app.get('/api/missing', (req, res) => {
// Return 404 Not Found status code
res.status(404).json({ error: 'Resource not found' });
});💡Fluent HTTP status code chaining using res.status().
🎯 Interview Key Takeaway
Chain res.status(code) before .json() or .send() to set explicit HTTP response status codes.
#res.status#HTTP Status#Chaining#Beginner
Back to all Express JS Questions→