0

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

1
  • 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 6:21
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'));
1
  • 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 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.