Express res.json() example
Posted on: October 19, 2021 by Deven
Once the server process the client request, the result needs to be sent back to the client. Express provides various methods to send the result like res.send(), res.json(), res.redirect(). In today’s post, we are going to see res.json().
res.json() is a utility wrapper around res.send() that also adds the Content-Type: application/json header to the response. This header is used by the frontend to determine the type of the response and parse it accordingly. JSON is widely used as a way to exchange data, so these methods are provided for convenience.
app.get('/client', (req, res) => {
res.json({
name: 'John Doe',
country: 'India'
});
});
Share on social media
//