我在尝试将中间件添加到应用程序时遇到以下错误:
throw new TypeError('Router.use() requires callback function');
MyMiddleware.js
module.exports = function(options) {
var err = checkOptions(options);
if(err) {
throw err;
}
var middleware = function(req, res, next) {
// do something here
}
return middleware;
}
这就是我添加中间件的方式:
var MyMiddleware = require('./server/middlewares/MyMiddleware');
var middleware = MyMiddleware(config);
router.use('/path', middleware);
app.use('/', router);
我正在使用express 4.x版本。
答案 0 :(得分:0)
您将模块定义为一个函数,您必须调用它。
var MyMiddleware = require('./server/middlewares/MyMiddleware')();