1

I serve a temporary directory using

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'));

At some point, I may delete /var/www/tmp and think it makes sense to stop serving its content both a static files and as an automatically built index page. Is there a way to do so ? I tried calling app.use with an undefined callback but it raises an error.

Thanks, Marc

ballatom
  • 123
  • Express does not provide a documented mechanism for removing routes that you've already installed. You'd have to hack into the route list to remove one or create a custom middleware that uses logic to decide whether to call the express.static() handler or not. – jfriend00 Feb 13 '19 at 06:21

1 Answers1

0

Based on jfriend00's answer, I have installed a first handler that tests if the directory exists :

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'));
ballatom
  • 123
  • Why would you want to call next('route') when you've already sent the response? I think you just remove the next('route') or remove the response.end()? One or the other. – jfriend00 Feb 14 '19 at 05:16