简体   繁体   中英

How to route to a function in sails.js

I wish to route to a function, rather than to a view (for integrating with a 3rd party API). With express, it would look like:

app.get('/auth/signup', passport.authenticate('dailycred'));

What would be the sails.js equivalent?

Or would it be recommended to accomplish this by creating a view and controller pair, that will invoke this function, rather than having the function somehow directly called?

Thanks!

It would be preferable to create View and controller pair as we can make more customizations.Sails.js equivalent would be

We can route to the function by adding this into the route.js file.

module.exports.routes = {
  '/auth/signup': {
    controller: 'authController',
    action: 'login'
  }
};

//Sample code for the AuthController.js where our function will be invoked

var AuthController = {
  'login': function(req, res, next) {
    passport.authenticate('google', {
        failureRedirect: '/login',
        scope: ['openid', 'email']
      },
      function(err, user) {
        req.logIn(user, function(err) {
          console.log("google")
          if (err) {
            res.view('500');
            return;
          }
          req.session.authenticated = true;
          res.redirect('/index');
          return;
        });
      })(req, res, next);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM