繁体   English   中英

节点js中出现错误“错误:发送后无法设置标头。”

[英]getting error in node js "Error: Can't set headers after they are sent."

说“我在节点 js 中遇到错误,可能是在登录时或代码崩溃时,即错误:发送后无法设置标头?”

err: Error: Can't set headers after they are sent.
      at validateHeader (_http_outgoing.js:491:11)
      at ServerResponse.setHeader (_http_outgoing.js:498:3)
      at basicAuthentication (/home/brainmobi/git/Abhijeet/Node/qarari_backend/lib/middleware/basicAuth.js:16:16)
      at Layer.handle [as handle_request] (/home/brainmobi/git/Abhijeet/Node/qarari_backend/node_modules/express/lib/router/layer.js:95:5)
      at trim_prefix (/home/brainmobi/git/Abhijeet/Node/qarari_backend/node_modules/express/lib/router/index.js:317:13)
      at /home/brainmobi/git/Abhijeet/Node/qarari_backend/node_modules/express/lib/router/index.js:284:7
      at Function.process_params (/home/brainmobi/git/Abhijeet/Node/qarari_backend/node_modules/express/lib/router/index.js:335:12)

在 Node.js 和几乎所有服务器语言中,在某些标头已经在单个响应中发送给客户端之后,您不能设置另一个标头。

例如,此用例将不起作用

app.get('/', (err, res) => {
res.send('Hello!'); // This will send a response to the client, along with the appropriate headers.

res.send('How are you?!'); // This will throw the same error as you are getting before a response along with some headers are already sent
}) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM