express 遇到问题 - Error: Can't set headers after they are sent

在node配合mongodb,链接数据库,查询所有数据2步操作,都有响应数据返回是会出现

Error: Can't set headers after they are sent,的报错

故障原因:引用

“Can’t set headers after they are sent.” => “不能发送headers因为已经发送过一次了” => 在处理HTTP请求时,服务器会先输出响应头,然后再输出主体内容,而一旦输出过一次响应头(比如执行过 res.writeHead()res.write()res.end()),你再尝试通过 res.setHeader()res.writeHead() 来设置响应头时(有些方法比如 res.redirect() 会调用 res.writeHead()),就会报这个错误。

(说明:express中的 res.header() 相当于 res.writeHead()res.send() 相当于 res.write()

原因就是你程序有问题,重复作出响应,具体原因很多,需要自己根据以上的原则来排除。

以下是我查到的链接

https://cnodejs.org/topic/53774ffecbcc396349ca1155

猜你喜欢

转载自www.cnblogs.com/herewego/p/9278920.html