node.js - 如何调试 Node.js 中的 header 发送错误

标签 node.js header

我收到很多发送后无法设置 header 错误,而且它们似乎从来没有在我的app.js中给我行号,这是正常的吗?人们如何调试这些错误?

我抛出 header 错误的代码看起来像这样,它是否在做一些奇怪的事情来隐藏行号?

app.get('/', function(req, res, next) {
    if (req.param('q')) {
        searchProvider.search(
            req.param('q'),
            function( error, results) {
                res.render('search', {
                    locals: {
                        results: results,
                        q: req.param('q')
                    },
                });
            }
        );
    } else {
        res.render('index');
    }
});

最佳答案

发送后无法设置 header

是一个常见错误,这意味着您基本上多次调用 res.renderres.endres.send。这意味着您试图将多个 HTTP 响应写入一个 HTTP 请求(这是无效的)。

此错误的一个常见原因是在一个中间件中两次调用 next

也许你有一个像这样的中间件

app.all("*", function(req, res, next) {
  // not logged in
  if (!req.user) {
    res.render("loginError");
  } 
  // bad accidental next call!! Will call next after rendering login error
  next();
});

关于node.js - 如何调试 Node.js 中的 header 发送错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7372667/

相关文章:

javascript - 正则表达式匹配字符串的前 n 个字符

node.js - 如何将查询逻辑添加到基于 graphql-sequelize 的设置中?

c++ - C++ 中没有的头函数

node.js - 超音速框架中的项目创建错误

javascript - 更新 Node js Promise 函数中变量的值

C++ 类没有名为 'blah' 的成员

c++ - C/C++ 私有(private)数组在头文件中初始化

c - 如何在 C 项目中导入您自己的 HEADER (.h) 文件

python - Scapy:如何获取完整的 IP 数据包 header ?

node.js - 您如何安装和运行 Mocha,Node.js 测试模块?安装后获取 "mocha: command not found"