Difference between res.send and res.json in Express.js

JavascriptFront End TechnologyObject Oriented Programming

Whenever an Express application server receives an HTTP request, it will provide the developer with an object, commonly referred to as res. For example,


app.get('/test', (req, res) => {
   // use req and res here

The res object basically refers to the response that'll be sent out as part of this API call.

The res.send function sets the content type to text/Html which means that the client will now treat it as text. It then returns the response to the client.

The res.json function on the other handsets the content-type header to application/JSON so that the client treats the response string as a valid JSON object. It also then returns the response to the client.

Published on 02-Dec-2019 07:00:35