【发布时间】:2017-11-30 11:46:26
【问题描述】:
我已经创建了一些具有一些逻辑的标准中间件,并且根据逻辑我需要调用一些 3rd 方中间件。
使用 app.use() 添加中间件,这是我添加自定义中间件的地方。
一旦在我的中间件中我不再能够访问 app.use(),我该如何调用中间件?
这是一些代码:
有什么想法吗?
const customerData = (req, res, next) => {
try {
console.log('Started');
if (process.env.STORE_CUSTOMER_DATA === 'true') {
// Here is some custom middleware that doesn't belong to me
//
// Returns a function (I confirmed it) ready to be called with res,req, next
//
let externalMiddlware = logger({
custom:true
});
// Do I return it ? Call it ? Trying everything and nothing seems to work
externalMiddlware(req,res,next); // ???
} else {
// DO not call external middleware, will break out of if and then call next()
}
console.log('Finished');
next();
} catch (err) {
next(err);
}
};
module.exports = customerData;
【问题讨论】:
标签: node.js express middleware