node返回数据出错 Cannot set headers after they are sent to the client

时间:2024-03-19 17:51:25
  • “Cannot set headers after they are sent to the client.”==>“发送到客户端后无法设置标头”
    node返回数据出错 Cannot set headers after they are sent to the client
  • 在处理HTTP请求时,服务器会输出响应头,再输出主体内容,如果你在代码中设置了res.writeHead()函数,然后再使用res.send()方法,就会出现这个错误
    node返回数据出错 Cannot set headers after they are sent to the client
  • 这时候只需要将res.send()函数更换为res.end()函数代为执行即可
    node返回数据出错 Cannot set headers after they are sent to the client
  • 注意,虽然res.end()和res.send()方法不太一样,但是技术只是一种工具,能解决问题就行,不需要执着某一个函数的使用