jon*_*bbs 5 javascript node.js express
我正在尝试编写快速中间件来检查授权标头中 JWT 的有效性。这看起来很简单,但我不希望它在所有路由上运行(例如不在登录/注册路由器上运行)。
因此,我想在路由器声明中指定路由应该需要有效的令牌。例如这样的东西
const controllers = require('../controllers');
module.exports = (app) => {
app.post('/auth/signup', controllers.auth.signup.post);
app.post('/auth/login', controllers.auth.login.post);
app.get('/teams', controllers.teams.get, {requiresToken:true});
};
Run Code Online (Sandbox Code Playgroud)
除此之外, .post 和 .get 不采用第三个参数,并且控制器仅采用 (req,res,next) 参数,因此我无法真正看到为每个路线传递起始数据的方法。我确信我错过了一些简单的东西
这就是我创建一个中间件来将数据传递到的方式
module.exports = function(options) {
return function (req, res, next) {
//write your code here
// here you can access options variable
console.log(options.data)
next();
}
}
Run Code Online (Sandbox Code Playgroud)
你如何称呼中间件是这样的
app.use(middleware({'data' : 'Test'}));
Run Code Online (Sandbox Code Playgroud)
根据路线使用
app.post('/userRegistration', middleware({'data' : 'Test'}), (req, res) => {});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5866 次 |
| 最近记录: |