I’m trying to call two middlewares in chain. The first one will passa a parameter to controller and second one doesn’t require it.
Following is my code –
routes.js
module.exports = (app) => {
app.get('/printrajesh',
AuthController.checkauthorization('xyz'),
RmController.printrajesh
)
}
The Authcontroller
module.exports = {
async checkauthorization (role){
console.log("Check Authorization is called")
console.log(role)
return function(req, res, next){
if(role != 'xyz'){
res.status(400).send({
error: 'You are not Authorized'
})
}else{
next()
}
}
}
}
The error I’m getting is –
Server is Up and Running...
Check Authorization is called
xyz
/home/tanmay/gmphbackend/node_modules/express/lib/router/route.js:211
throw new Error(msg);
^
Error: Route.get() requires a callback function but got a [object Promise]
at Route.<computed> [as get] (/home/tanmay/gmphbackend/node_modules/express/lib/router/route.js:211:15)
at app.<computed> [as get] (/home/tanmay/gmphbackend/node_modules/express/lib/application.js:499:19)
at module.exports (/home/tanmay/gmphbackend/src/routes.js:19:9)
at Object.<anonymous> (/home/tanmay/gmphbackend/src/app.js:14:20)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47
Node.js v18.16.1
Not Sure what I am doing wrong here. Please help. Thanks