What is actual difference between res.send
and res.json
as both seems to perform same operation of responding to client.
相关问题
- Angular RxJS mergeMap types
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
The methods are identical when an object or array is passed, but
res.json()
will also convert non-objects, such asnull
andundefined
, which are not valid JSON.The method also uses the
json replacer
andjson spaces
application settings, so you can format JSON with more options. Those options are set like so:And passed to a
JSON.stringify()
like so:This is the code in the
res.json()
method that the send method doesn't have:The method ends up as a
res.send()
in the end:Looking in the headers sent...
res.send uses content-type:text/html
res.json uses content-type:application/json
https://github.com/visionmedia/express/blob/ee228f7aea6448cf85cc052697f8d831dce785d5/lib/response.js#L174
res.json
eventually callsres.send
, but before that it:json spaces
andjson replacer
app settings