0

How do I make it so that if a response has been sent back, then no more responses should be sent? Actually, the issue is that if a response is sent back, then express (or nodejs) shouldn't continue running through the rest of the code.

I've tried doing next() but terminal throws the error of next() being undefined. res.end() doesn't seem to work either?

routing.js:

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

user.js createUser

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 createUser

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");
    }
}

Call createUser in routing, which then calls checkAllInput which calls checkUsername but should stop if username sends a response back.

user1883614
  • 905
  • 3
  • 16
  • 30
  • Show some code, please. – Paul Feb 13 '15 at 04:33
  • Those code you posted should work find, though it is worrying because the `else` there is not implemented, so you will simply never respond to some requests... We'll need to see a full example of your route handler. – loganfsmyth Feb 13 '15 at 05:17
  • There's nothing special about my route handler. Just your basic routing using express. router.post('/user/create', function(req, res) { user.createUser(req, res); }); and createUser takes in the req, then calls the functions above. – user1883614 Feb 13 '15 at 05:19

2 Answers2

0

You need to return, so the code stops there. Otherwise it will keep on going.

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() isn't inherent in the code, it has to be defined somewhere, and even if it is, it still doesn't stop the code as it is asynchronous.

I'm assuming you're using Express. You might want to do this with middleware.

//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);
Brian Noah
  • 2,962
  • 18
  • 27
0

You want to use the express middleware like so :

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  
}

Add in sequence you want to handle the request.

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

So what will happen the express router will first call checkUserName and if you don't call next() then it returns. So if you call next() in it it will call the next method in for the current requested resource which is checkPassword. And so on.

Take a look at Having a hard time trying to understand 'next/next()' in express.js thread.

Note that you don't have to use return. Also take a look at @Brian answer

Community
  • 1
  • 1
eenagy
  • 912
  • 1
  • 8
  • 22