res.send 和 res.json 之间的实际区别在于似乎执行相同的操作
What is actual difference between res.send and res.json as both seems to perform same operation of responding to client.
推荐答案方法在传递对象或数组时是相同的,但 res.json ()还将转换非对象,例如 null 和 undefined 无效的JSON。
The methods are identical when an object or array is passed, but res.json() will also convert non-objects, such as null and undefined, which are not valid JSON.
该方法还使用 json replaceacer 和 json spaces 应用程序设置,因此您可以使用更多选项格式化JSON。这些选项设置如下:
The method also uses the json replacer and json spaces application settings, so you can format JSON with more options. Those options are set like so:
app.set('json spaces', 2); app.set('json replacer', replacer);并传递给一个 JSON.stringify()像这样:
JSON.stringify(value, replacer, spacing); // value: object to format // replacer: rules for transforming properties encountered during stringifying // spacing: the number of spaces for indentation这是发送方法没有的 res.json()方法中的代码有:
This is the code in the res.json() method that the send method doesn't have:
var app = this.app; var replacer = app.get('json replacer'); var spaces = app.get('json spaces'); var body = JSON.stringify(obj, replacer, spaces);该方法最终作为 res.send()最后:
this.charset = this.charset || 'utf-8'; this.get('Content-Type') || this.set('Content-Type', 'application/json'); return this.send(body);更多推荐
Express.js中res.send和res.json之间的区别
发布评论