Udit Sarin
Udit Sarin

Reputation: 59

NODEJS - Reroute on same Url

I am kind of stuck in something. Please help me

The problem i want to solve is -

I am getting some post parameters and i want to route based on those parameters in NodeJs. Now the issue is when i use switch case to route on base of the post params rerouting is not happening.

router.post('/', function (req, res, next) {
    var method = req.body.method;
    switch (method) {
        case 'register_user':
            router.post('/', userController.registerUser);
            break;
        case 'user_login':
            router.post('/', userController);
            break;
}
});

Upvotes: 0

Views: 392

Answers (1)

hankchiutw
hankchiutw

Reputation: 1662

Your rerouting code inside switch context just appends more middlewares on the path /, not actually do the route as you think.

Revise like this:

router.post('/', function (req, res, next) {
    var method = req.body.method;
    switch (method) {
        case 'register_user':
            // returned since you want route to the function
            return userController.registerUser(req, res, next);
            break;
        case 'user_login':
            return userController(req, res, next);
            break;
    }
});

Upvotes: 2

Related Questions