javascript - Node JS 发送后无法设置 header

标签 javascript node.js http header

我正在编写一个 Node.js 应用程序。由于某种原因,我可以加载页面一次,它可以工作。然后我收到一条错误消息“发送后无法设置 header ”

任何帮助将不胜感激!谢谢。

这是我的代码:

module.exports = {

login: function(req, res) {

    login.parseLogin(function(status) {

        if(status != true) {
            res.end();
        }

        res.send('{"Success": ' + true + '}');
        res.end();

    }, "Robert", "C");

},

signup: function(req, res) {

    signup.parseSignup(function(status) {

        if(status != true) {
            res.send(' {"Failed to create account": ' + new Date() + '} ');
            res.end();
        }

        res.send(' {"Account": ' + username + ',"createdAt": ' + new Date() + '} ');
        res.end();

    });

},


addbit: function(req, res) {
    console.log("Addbit");
},


removeBit: function(req, res) {
    console.log("RemoveBit");
},

queryBits: function(req, res) {
    console.log("QueryBits");
}

};

最佳答案

登录和注册功能中的问题是您检查错误条件,结束响应,然后再次发送响应。

    if(status != true) {
        res.end();
    }

应该是

   if(status != true) {
        return res.end();
    }

在这些函数中的一个或多个中,第一次加载页面时该值为 true,而下一次则为 false。

在注册中,它应该是:

if(status != true) {
        res.send(' {"Failed to create account": ' + new Date() + '} ');
        return res.end();
    }

关于javascript - Node JS 发送后无法设置 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31763860/

相关文章:

javascript - 如何在angularjs中访问ng repeat之外的项目?

javascript - 传递 Javascript 问号运算符多个语句?

node.js - 错误 : (gcloud. app.deploy)错误响应:[12] 此项目未启用连接到外部网络

javascript - 停止所有代码然后继续,就像 JS 中的 alert()

javascript - react-bootstrap 中 FormControl 和 ControlLabel 之间的高度不同

javascript - 动态 JSON 引用 Node.js

node.js - NodeJS 中的 Mongoose 模式类型错误 : Schema is not a constructor

angular - 在浏览器控制台Angular 8中禁用HTTP方法服务器的错误

java - Java 1.5 下 HTTP 连接超时

c# - 如何在 GET 请求中包含内容?