res.json()
noun
say it: “REZ dot JAY-sahn” /rɛz dɑt ˈdʒeɪsɑn/
An Express method that sends a value back to the caller as JSON, setting the right header and finishing the response.
What does res.json() do in Express?
res.json() sends a JavaScript value back as the answer to a request. In one call it turns the value into JSON text, sets the Content-Type header to application/json and ends the response.
app.get("/menu", (req, res) => {
res.json([{ item: "Bagel", price: 2.5 }]);
});
// the browser gets: [{"item":"Bagel","price":2.5}]
In a sentence
- “Calling res.json() twice in one route throws an error, because the first call already ended the response.”
- “The orders route ends with res.json(), so the app gets the list as JSON.”
Level: Apprentice · More Node.js terms at this level
Updated