Luc
Luc

Reputation: 17072

Calling controller's action from another controller in expressjs

In my expressjs / sailsjs app, I have 2 controllers:

- MainController only used to display the pages (for instance /signin route displays the signin page)
- ProxyController that handles several action (API calls)

In one action of ProxyController I need to redirect to the signin page, I use the res.redirect('/signin') but nothing happens.

In the routes I have:

    '/signin' : {
       controller: 'main',
       action: 'signin'
    },

Any idea on how can I call the MainController's signin action from ProxyController ?

Upvotes: 4

Views: 6005

Answers (2)

Gene Bo
Gene Bo

Reputation: 12063

I had the same question - how to call one controller from another controller in Express.

I tried this - where the code is in a helper controller that will in turn call LoginCntrlr - and it did the trick:

var rootPath = '..';
var rendering = require(rootPath + '/util/rendering'),
    loginController = require(rootPath + '/controllers/login');

exports.mySecondaryLogin = function(req, res) {

    // access get params
    console.log('user type param "ut" is : ' + req.query.ut);

    // Set login params and send to check Login (a POST request)
    req.body.un = 'user@acme_someplace.com';
    req.body.pw = '@@@@';

    return loginController.checkLogin(req, res);
}

Upvotes: 3

Hector Correa
Hector Correa

Reputation: 26690

Does the client calling the ProxyController knows how to process an HTTP Redirect?

I suspect you are using an AJAX call to hit the proxy, if that's the case then you'll need to evaluate the HTTP response and do the redirect. Browsers do this automatically, but not so AJAX calls.

Upvotes: 2

Related Questions