美文网首页
res.send( )和res.json( )的区别

res.send( )和res.json( )的区别

作者: 放开那小超 | 来源:发表于2018-02-05 15:36 被阅读0次

返回JSON格式推荐使用res.json

当传递对象或数组时,这两个方法是相同的,但是res.json()也会转换非对象,如null和undefined,这些无效的JSON。

该方法还使用json replaceacer和json spaces的设置,因此您可以使用更多选项格式化JSON。 例如:

app.set('json spaces', 2);
app.set('json replacer', replacer);

传递给JSON.stringify()类似:

JSON.stringify(value, replacer, spacing);
// value: 需要格式化的对象
// replacer: stringify 时如何转化属性的规则
// spacing: 锁紧的空格数量

res.json方法的中res.send部分没有的代码:

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);

相关文章

  • res.send( )和res.json( )的区别

    返回JSON格式推荐使用res.json 当传递对象或数组时,这两个方法是相同的,但是res.json()也会转换...

  • Express 二次set header 问题

    2017/08/22 开发Express+MongoDB时发现res.send, res.json应该都是异步执行...

  • node学习之session犯的低级错误

    一开始把代码写成这样: 死活获取不到session。。。 后来往res.send后面又加了一句res.send想测...

  • res.json()

    问题描述 前端向后端发请求,返回res,打印res的值,使用await res.json(),但不清楚res.js...

  • res.json

    后面需要括号包裹需要返回的数据 没包裹的将不会返回

  • express res.write()和res.send()

    res.write()和res.send() 相同点:功能基本相同,用来输出相应的返回数据 不同点:对参数要求不同...

  • react数据交互

    ch('url接口地址',{配置信息).then(res=>{return res.json() //将数据转换成...

  • 4.res.json、res.send

    1.res.json、res.send https://blog.csdn.net/qq_36410795/art...

  • response方法

    res.json([body]) 发送JSON响应。此方法发送响应(具有正确的内容类型),该响应是使用JSON.s...

  • 认识 Express 的 res.send() 和 res.en

    前言 在使用 Node.js 的服务端代码中,如果使用的是 Express 框架,那么对于一个请求,常常会有两种响...

网友评论

      本文标题:res.send( )和res.json( )的区别

      本文链接:https://www.haomeiwen.com/subject/xhqizxtx.html