javascript - app.use 在发送错误后出现 Can\t set headers 错误

标签 javascript node.js express

在我的 app.js 中我这样做

app.use(function(req, res, next){

  if(!req.user){
    res.redirect('/login_');
  }
  next();
})

我没有看到任何错误,在route/index.js 中我看到了

router.get('/login_', function(req, res) {
    res.render('login', { user : req.user });
});

但是我收到了 throw new Error('Can\t set headers after they are sent.'); 的错误 我知道这是由于请求未结束引起的,但是我上面的代码有什么问题吗?对这个错误一无所知。

route/index.js 完整代码 http://pastebin.com/kT2QfnjL

最佳答案

这是因为代码在发送响应后尝试发送响应时造成的。

引用:

Error: Can't set headers after they are sent to the client

关于javascript - app.use 在发送错误后出现 Can\t set headers 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38805638/

相关文章:

javascript - 使用 JQuery 用数据填充现有表

javascript - jQuery:在点击时检索 DOM 的 ID 仅返回第一次点击的 ID

javascript - Colorbox 调整大小不起作用,返回 "TypeError: $.fn.colorbox is undefined"

javascript - 替换 contenteditable div 中的选定文本

Node.js mongodb updateMany 与索引

javascript - Node.js 如何处理服务器的下拉?

node.js - 使用自定义源代码替换默认的 Express/Connect JS 文件

javascript - vue.js 模块中的数据范围

node.js - Angular Route 在浏览器刷新时显示原始 JSON

node.js - GET 请求在 Node 和 Express App 中返回 html