【问题标题】:Express continues to send responses after sending the first oneExpress 发送第一个后继续发送响应
【发布时间】:2015-02-13 22:11:43
【问题描述】:

我该如何做到,如果已发回响应,则不应再发送响应? 实际上,问题在于,如果发回响应,则 express(或 nodejs)不应继续运行其余代码。

我尝试过 next() 但终端抛出 next() 未定义的错误。 res.end() 似乎也不起作用?

routing.js:

router.post('/user/create', function(req, res, next) {
    user.createUser(req, res);
});

user.js 创建用户

user.prototype.createUser = function(req, res, next) {
    var body = req.body;
    checkAllInput(body, res, next);
    // do some more checks then finally create user
}

user.js 创建用户

function checkAllInput(body, res, next) {
    checkError.checkUsername(body, res, next);
    checkError.checkPassword(body, res, next);

}

checkError.js

userClass.prototype.checkUsername = function(username, res) {
    if (!username || username === "bob) {
        res.status(406).send("username");
    }
}

userClass.prototype.checkPassword = function(password, res) {
    if (!password || password === "hello") {
        res.status(406).send("password");
    }
}

在路由中调用 createUser,然后调用 checkAllInput 调用 checkUsername 但如果用户名发送回响应则应该停止。

【问题讨论】:

  • 请显示一些代码。
  • 您发布的那些代码应该可以找到,但令人担忧的是,因为 else 没有实现,所以您将永远不会响应某些请求...我们需要查看完整的您的路由处理程序的示例。
  • 我的路由处理程序没有什么特别之处。只是您使用 express 的基本路由。 router.post('/user/create', function(req, res) { user.createUser(req, res); });并 createUser 接受 req,然后调用上面的函数。

标签: node.js express


【解决方案1】:

你需要返回,所以代码停在那里。否则它将继续进行。

userClass.prototype.checkUsername = function(username, res) {
    if (!username || username === "bob) {
        return res.status(406).send("username");
    }
}

userClass.prototype.checkPassword = function(password, res) {
    if (!password || password === "hello") {
        return res.status(406).send("password");
    }
}

next() 不是代码中固有的,它必须在某个地方定义,即使是,它仍然不会停止代码,因为它是异步的。

我假设您使用的是 Express。您可能希望使用中间件来执行此操作。

//middleWare.js

exports.checkUserModel = function (req, res, next) {
    var body = req.body,
        username = body.username,
        password = body.password,

    if (!username || username === "bob) {
        return res.status(406).send("username required");
    }

    if (!password || password === "hello") {
        return res.status(406).send("password required");
    }
    next();
}

//router.js
var middleWare = require('./middleWare');
var user = require('./controllers/users');
app.post('/user/createUser', middleWare.checkUserModel, user.create);

【讨论】:

    【解决方案2】:

    你想像这样使用 express 中间件:

    checkUsername = function(req, res, next) {
       if (checkUserNameIsValid) {
          //check the password
          next()
       }
       else{ 
    ///otherwise return
          res.status(406).send("username");
         }
    }
    
    checkPassword = function(req, res, next) {
        if (checkIfPasswordIsValid) {
            //create the user when password is valid too
           next();
        }
        else { 
          //else return response
          res.status(406).send("password required");
        }
    }
    createUserIfPasswordAndUserNameIsOk = function(req, res, next){
         //create the user here  
    }
    

    按顺序添加你要处理的请求。

    router.post('/user/create', checkUserName, checkPassword, createUserIfPasswordAndUserNameIsOk  );
    

    那么快速路由器会首先调用 checkUserName 会发生什么,如果你不调用 next() 然后它会返回。因此,如果您在其中调用 next(),它将为当前请求的资源调用下一个方法,即 checkPassword。等等。

    看看Having a hard time trying to understand 'next/next()' in express.js线程。

    请注意,您不必使用 return。也可以看看@Brian answer

    【讨论】:

    • 感谢您的参考。 :)
    猜你喜欢
    • 2011-04-19
    • 1970-01-01
    • 2018-09-19
    • 2021-10-11
    • 2022-10-08
    • 2021-02-20
    相关资源
    最近更新 更多