我試圖在我的express js應用程序中實現中間件功能,它遵循一個簡單的MVC體系結構。Express js:不調用中間件功能
routes.js
下route
夾
//load the controller auth.js
var auth = require('../controllers/auth');
//auth route
router.route('/auth').get(auth.simpleAuth);
auth.js
下controllers
夾
//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
下middleware
夾
module.exports={
testMiddleware:function (req,res,next) {
console.log('inside middleware');
if(req.username == true){
next();
}else{
console.log('auth failed')
}
}
}
沒有錯誤消息是但是當我訪問auth
路由時,不會調用中間件功能。
請看看expressjs中間件https://expressjs.com/en/guide/using-middleware。 html –