node——小坑、小技巧

1、express中res.end 、res.send、res.json返回数据的区别

      用于快速结束没有任何数据的响应,使用res.end()。 
      响应中要发送JSON响应,使用res.json()。 
      响应中要发送数据,使用res.send() ,但要注意header ‘content-type’参数。 
      如果使用res.end()返回数据非常影响性能。

2、body-parse

使用body-parse来解析POST传递过来的数据时,在app.js文件中添加
app.use( bodyParser.urlencoded({extended: true}) );//解析application/x-www-form-urlencoded
app.use(bodyParser.json());//解析json数据依赖于urlencoded模块 必须同时应用
app.use(bodyParser.text());//将所有的数据以文本格式字符串的返回 form表单中 text-plain
app.use(bodyParser.raw());//解析二进制数据

//设置完毕之后,会在req对象上面新增一个req.body的一个对象

猜你喜欢

转载自blog.csdn.net/YUHUI01/article/details/83957653