0

Express middleware issue

Check the code and tell me why the middleware is throwing error but when i use it inside function it works perfectly. https://code.sololearn.com/WS3NM1z60WvG/?ref=app

15th Jun 2020, 12:28 AM
Akib
Akib - avatar
3 Respostas
+ 6
Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function (next) in the application’s request-response cycle. Middleware is accountable for calling the next middleware by making call function of next(), before it can pass req and res to the next function. auth.js middleware should be in this format module.exports = function(req,res,next) { if (req.session.role === 'admin') { next(); // responsible for triggering next function call } else { res.render('unauthorized'); // not go to next function } } before you can use it as const auth = require('../auth/auth'); app.get("./auth', auth, (req,res) => { res.render('protected'); // next function execution here (passing req,res) }
15th Jun 2020, 1:51 AM
Calviղ
Calviղ - avatar
15th Jun 2020, 1:08 PM
Gordon
Gordon - avatar
+ 1
Well, yes what you've said i know. I was trying to use the class method instead of a regular function. I have found that it works only in the function format. Thanks for the suggestion. function authenticate(req, res, next){ adminAuth.authenticate(req, res, next); }
15th Jun 2020, 2:26 AM
Akib
Akib - avatar