send方法作用
- send方法内部会检测响应内容的类型
- send方法会自动设置http状态码
- 二进制Binary内容:参数为Buffer(缓冲)时,Content-Type 会被设置为 “application/octet-stream”
1
res.send(newBuffer('whoop'));
- 字符串:Content-Type 默认设置为”text/html”:
1
res.send('hello');
- Array/Object:返回json,Content-Type 设置为application/json;
1
res.send({name:张三,age:23});
res.send()和res.end()区别
- 参数
- send: 可传参数类型:Buffer对象 string对象 Array
- end:(data,encoding,callback)
- data:只能发送Buffer和string的数据
- encodeding 类型的数据
- 是否设置内容类型和状态码
- send可以
- end需要手动设置res.writeHead({})
本文作者:
SparkParis
本文链接: https://sparkparis.github.io/2020/04/08/Express1-Send%E6%96%B9%E6%B3%95%E8%AF%A6%E8%A7%A3/
版权声明: 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。转载请注明出处!
本文链接: https://sparkparis.github.io/2020/04/08/Express1-Send%E6%96%B9%E6%B3%95%E8%AF%A6%E8%A7%A3/
版权声明: 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。转载请注明出处!