Several individual static directories with different paths using Express

I would like to be able to get Express to treat several directories (not just one) as "static" -- that is, if the file is there, then serve it.

Connect's static() module seems to be geared up for people who want to make files in a specific directory available in the server's root. However, that's not what I want. What I am after, is end up with something like this:

  • GET /modules/MODULE1 -> Return files in modules/MODULE1/public
  • GET /modules/MODULE2 -> Return files in modules/MODULE2/public
  • GET /modules/MODULE3 -> Return files in modules/MODULE3/public

I am looking at the source of static, which in turns uses send, which in turns defines SendStream, which takes the file path straight from the request (which is not what I want).

Are there easy ways to do this?

Merc.

what's wrong with

app.use('/modules/MODULE1', express.static('modules/MODULE1/public'))

for each module?

The answer is here:

https://groups.google.com/forum/?fromgroups=#!topic/express-js/kK9muR0mjR4

Basically:

var st = express.static(
    __dirname + app.set('path.static'), 
    { maxAge : app.set('static.expiry') }
);

app.get(/^\/static\/(v.+?\/)?(.+$)/, function (req, res, next) {
  req.url = req.params[1];
  st(req, res, next);
});

Basically, since Static consider req.url, it's a matter of hacking it so that it "looks right" when/if the path matches.

I asked TJ if he would add it as an option, he (rightly) answered:

this isn't something send() should do, you can already do this easily with connect/express with several static() middleware, you would just have to do the same but more manual with send()

https://github.com/visionmedia/send/issues/10#issuecomment-8225096