What is actual difference between res.send
and res.json
as both seems to perform same operation of responding to client.
This question is related to
javascript
node.js
http
express
res.json
forces the argument to JSON. res.send
will take an non-json object or non-json array and send another type. For example:
This will return a JSON number.
res.json(100)
This will return a status code and issue a warning to use sendStatus.
res.send(100)
If your argument is not a JSON object or array (null,undefined,boolean,string), and you want to ensure it is sent as JSON, use res.json
.
Looking in the headers sent...
res.send uses content-type:text/html
res.json uses content-type:application/json
edit: send actually changes what is sent based on what it's given, so strings are sent as text/html, but it you pass it an object it emits application/json.
res.json
eventually calls res.send
, but before that it:
json spaces
and json replacer
app settingsSource: Stackoverflow.com