栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Express.js中res.send和res.json之间的区别

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Express.js中res.send和res.json之间的区别

传递对象或数组时,方法是相同的,但

res.json()
也会转换非对象,例如
null
undefined
,它们是无效的JSON。

该方法还使用

json replacer
json spaces
应用程序设置,因此您可以使用更多选项来格式化JSON。这些选项设置如下:

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()
send方法没有的方法中的代码:

var app = this.app;var replacer = app.get('json replacer');var spaces = app.get('json spaces');var body = JSON.stringify(obj, replacer, spaces);

该方法最终以a

res.send()
结尾:

this.charset = this.charset || 'utf-8';this.get('Content-Type') || this.set('Content-Type', 'application/json');return this.send(body);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/436584.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号