简体   繁体   English

如何路由到sails.js中的函数

[英]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). 我希望路由到一个函数,而不是视图(与第3方 API集成)。 With express, it would look like: 使用express,它将看起来像:

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

What would be the sails.js equivalent? 什么是sails.js等效项?

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 创建View和controller对将是更可取的,因为我们可以进行更多的自定义.Sails.js相当于

We can route to the function by adding this into the route.js file. 我们可以通过将其添加到route.js文件中来路由到该函数。

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

//Sample code for the AuthController.js where our function will be invoked //用于调用我们的函数的AuthController.js的示例代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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