How do I set up static serving in Express with an arbitrary start path?

爷,独闯天下 提交于 2019-12-11 07:38:21

问题


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*', ..);


来源:https://stackoverflow.com/questions/12808295/how-do-i-set-up-static-serving-in-express-with-an-arbitrary-start-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!