devnotes.

res.json()

res.json() converts a JavaScript object or array into JSON and sets the Content-Type header to application/json.

const express = require("express");
const app = express();

app.get("/products/1", (req, res) => {
    res.json({ id: 1, name: "Wireless Mouse", price: 19.99 });
});

app.listen(4600, () => console.log("Listening on 4600"));

Common uses

  • Building JSON REST API responses.
  • Returning structured error objects.
  • Sending arrays of resources to a frontend.
res.json() | DevNotes