【发布时间】:2017-08-25 02:42:59
【问题描述】:
这是我在快递中面临的问题。 在我的快速中间件的某个地方,我想检查文件是否存在。
//Setting up express middeleware...
app.use(f1);
app.use(f2);
...
function f1(req, res, next) {
...
//Here I want to check if 'somefile' exists...
fs.access('somefile', callback1, req, res, next);
}
//In the callback, I want to continue with the middleware...
function callback1(err, req, res, next) {
if (err) {
//Report error but continue to next middleware function - f2
return next();
}
//If no error, also continue to the next middleware function - f2
return next();
}
function f2(req, res, next) {
}
如何将 req、res、next 作为参数传递给 fs.access 的回调? 上面的代码不起作用。我怀疑我需要使用闭包,但如何使用?
看待问题的一种完全不同的方式是:例如,我如何将 fs.access 本身用作快速中间件函数?
【问题讨论】:
-
我认为最好描述您正在尝试做的事情,因为您目前正在尝试做的事情没有多大意义......
标签: node.js express node-modules