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


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

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.

Updated on: 02-Dec-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements