如何在Express中使用任意开始路径设置静态服务? [英] How do I set up static serving in Express with an arbitrary start path?
问题描述
如果我想将目录.../whatever/stuff
设置为静态服务,但引用为http://example.com/mystuff
,则尝试这样做:
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"));
});
此大部分都有效,但是如果我引用mystuff的子目录时没有结尾斜杠(例如http://example.com/mystuff/subdir
),则会将其重定向到错误的位置(http://example.com/subdir/
),从而产生404.对于目录列表而言,这尤其成问题,因为目录中间件不会在指向子目录的链接上添加斜线.
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?)
推荐答案
尝试一下:
app.use('/mystuff*', ..);
这篇关于如何在Express中使用任意开始路径设置静态服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!