# res.send

body 参数可以是 Buffer 对象、String、对象、BooleanArray

# 概要

res.send([body])

# 描述

发送 HTTP 响应。

body 参数可以是 Buffer 对象、String、对象、BooleanArray。例如:

res.send(Buffer.from('whoop'))
res.send({ some: 'json' })
res.send('<p>some html</p>')
res.status(404).send('Sorry, we cannot find that!')
res.status(500).send({ error: 'something blew up' })

此方法对简单的非流式响应执行许多有用的任务:例如,它自动分配 Content-LengthHTTP 响应头字段(除非之前定义)并提供自动 HEAD 和 HTTP 缓存新鲜度支持。

当参数为 Buffer对象时,该方法将 Content-Type响应头字段设置为 "application/octet-stream",除非之前定义如下所示:

res.set('Content-Type', 'text/html')
res.send(Buffer.from('<p>some html</p>'))

当参数为 String时,该方法将 Content-Type设置为 "text/html":

res.send('<p>some html</p>')

当参数为 ArrayObject时,Express 以 JSON 表示形式响应:

res.send({ user: 'tobi' })
res.send([1, 2, 3])
Last Updated: 3/22/2023, 7:27:28 PM