res.status() sets the HTTP status code for the response and returns res, so it chains
neatly with res.send() or res.json().
const express = require("express");
const app = express();
app.get("/products/:id", (req, res) => {
const found = req.params.id === "1";
if (!found) {
return res.status(404).json({ error: "Product not found" });
}
res.status(200).json({ id: 1, name: "Wireless Mouse" });
});
app.listen(4600, () => console.log("Listening on 4600"));
Common uses
- Signaling success (
200,201) or failure (400,404,500) explicitly. - Building consistent, predictable REST API responses.
- Pairing with
res.json()/res.send()for a complete response.