[英]NodeJS Express Passport multiple files
I am trying to use passport in an Express app but getting a bit stuck trying to reference my passport strategies when my APIs/routing handlers are defined in multiple files. 我正在尝试在Express应用中使用通行证,但是在多个文件中定义了我的API /路由处理程序时,尝试引用我的通行证策略会有些卡住。
I have a structure like: 我有一个像这样的结构:
I set up the passport initialisation and define my passport strategies in app.js but then in my users.js and food.js I want to define APIs that use these strategies to protect my APIs. 我设置了护照初始化并在app.js中定义了我的护照策略,但是随后在我的users.js和food.js中,我想定义使用这些策略来保护我的API的API。 I cannot figure how to to reference the code I already created in those separate files.
我无法弄清楚如何引用这些单独文件中已经创建的代码。
Can anyone help? 有人可以帮忙吗?
use a middlewares function that authenticates every request to api. 使用中间件函数来验证对api的每个请求。
function middleware(req,res,next){
if(req.session && req.session.user){
next();
}else{
return res.json({
response:"login"
})
}
}
route.get('/', middleware , function(){
//rest of the code
});
route.post('/', middleware , function(){
//rest of the code
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.