Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

400
Views
Express js:middleware function is not invoked

i am trying to implement middle ware function in my express js application it follows a simple MVC architecture.

routes.js under route folder

//load the controller auth.js
var auth = require('../controllers/auth');
//auth route
router.route('/auth').get(auth.simpleAuth);

auth.js under controllers folder

//load the middleware
var middleware = require('../middleware/middleware');

module.exports={
  simpleAuth:function (req,res) {
       //invoke middleware
        middleware.testMiddleware;
        res.send('middleware test completed');
    }
}

middleware.js under middleware folder

module.exports={
    testMiddleware:function (req,res,next) {
        console.log('inside middleware');
        if(req.username == true){
            next();
        }else{
            console.log('auth failed')
        }

    }
}

There is no error message is shown but when i access auth route the middle ware function is not invoked.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You are not invoking it properly. Try calling it from route.

routes.js

var middleware = require('../middleware/middleware');

router.route('/auth').all(middleware.testMiddleware).get(auth.simpleAuth);

middleware.js

testMiddleware: function (req,res,next) {
    console.log('inside middleware');
    if(req.username == true) {
        next();
    } else {
        // error
        return res.status(401).json({ "message" : "auth failed"});
    }
}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!