我对快递中的中间件软件有疑问。
我想在一个中间件中有很多想法。例如
我的中间件有此代码
module.exports = function(req,res,next) {
if(req.method === 'GET') {
res.end('GET method not supported');
} else {
next();
}
};
我这样使用
app.route('/', <the_middleware>, (res, req, next) => {
// Code
})
但是我想知道是否可以做这样的事情
app.route('/', <the_middleware>.<the function1>, (res, req, next) => {
// Code
})
app.route('/', <the_middleware>.<the_function2>, (res, req, next) => {
// Code
})
有可能做类似的事情
function function1 (req,res,next) {
if(req.method === 'GET') {
res.end('GET method not supported');
} else {
next();
}
};
function function2 (req,res,next) {
if(req.method === 'GET') {
res.end('GET method not supported');
} else {
next();
}
};
module.exports = <I don`t know what go here>
谢谢。
更新。 IT有效,我的代码现在是
路由器
router.post('/', checkAuth.sayHi, checkAuth.sayBye, (req, res, next) => {
console.log('good');
res.status(200).send('it works');
console.log('yes');
});
中间件
module.exports = {
sayHi(req, res, next) {
console.log('hi');
next();
},
sayBye(req, res, next) {
console.log('bye')
next();
}
};
答案 0 :(得分:1)
您可以仅导出包含两个函数的对象:
module.exports = {
function1,
function2
}