This content originally appeared on CodeSource.io and was authored 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'
});
});
The post Express res.json() example appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Deven
Deven | Sciencx (2021-10-19T05:00:28+00:00) Express res.json() example. Retrieved from https://www.scien.cx/2021/10/19/express-res-json-example/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.