
我使用以下方式提供临时目录
var express = require("express");
var serveIndex = require("serve-index");
app.use('/tmp_dir', express.static('/var/www/tmp'));
app.use('/tmp_dir', serveIndex('/var/www/tmp'));
在某些时候,我可能会删除 /var/www/tmp,并认为停止以静态文件和自动构建的索引页的形式提供其内容是有意义的。有办法吗?我尝试使用未定义的回调调用 app.use,但它引发了一个错误。
谢谢,马克
答案1
根据 jfriend00 的回答,我安装了第一个处理程序来测试目录是否存在:
app.use('/tmp_dir', function(request, response, next) {
if (fs.existsSync('/var/www/tmp') === true) {
next();
} else {
response.end('DELETED');
next('route');
}
});
app.use('/tmp_dir', express.static('/var/www/tmp'));
app.use('/tmp_dir', serveIndex('/var/www/tmp'));