The res.json()
function should be sufficient for most cases.
app.get('/', (req, res) => res.json({ answer: 42 }));
The res.json()
function converts the parameter you pass to JSON using JSON.stringify()
and sets the Content-Type
header to application/json; charset=utf-8
so HTTP clients know to automatically parse the response.