我希望路由到函数,而不是 View (用于与 3rd party API 集成)。使用express,它看起来像:
app.get('/auth/signup',
Passport.authenticate('dailycred'));
sails.js 会是什么?等价?
或者是否建议通过创建一个 View 和 Controller 对来完成此操作,这将调用此函数,而不是以某种方式直接调用该函数?
谢谢!
最佳答案
最好创建 View 和 Controller 对,因为我们可以进行更多自定义。Sails.js 等效项是
我们可以通过将其添加到route.js 文件中来路由到该函数。
module.exports.routes = {
'/auth/signup': {
controller: 'authController',
action: 'login'
}
};
//将调用我们的函数的 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);
}
}
关于javascript - 如何路由到 sails.js 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19884048/