How do I set up static serving in Express with an

2019-08-04 13:14发布

问题:

If I want to set up the directory .../whatever/stuff to be served statically, but referenced as http://example.com/mystuff, I tried doing this:

app.configure(function() {
    app.use('/mystuff', _express.static(__dirname + "/whatever/stuff"));
    app.use('/mystuff', _express.directory(__dirname + "/whatever/stuff"));
});

This mostly works, but if I reference a subdirectory of mystuff without a trailing slash, say http://example.com/mystuff/subdir, it redirects to the wrong place (http://example.com/subdir/), resulting in a 404. This is especially problematic with directory listings, since the directory middleware doesn't put a trailing slash on links to subdirectories.

Is there something I can do to get around this? (and is my syntax above correct?)

回答1:

try this:

app.use('/mystuff*', ..);