快速静态文件 – 所有路由前缀

在我的代码,我现在有

app.use(express.static(__dirname + "/../styles")) app.use(express.static(__dirname + "/../public")) app.use(express.static(__dirname + "/../scripts")) app.use("/flosses", express.static(__dirname + "/../styles")) app.use("/flosses", express.static(__dirname + "/../public")) app.use("/flosses", express.static(__dirname + "/../scripts")) app.use("/flosses/edit", express.static(__dirname + "/../styles")) app.use("/flosses/edit", express.static(__dirname + "/../public")) app.use("/flosses/edit", express.static(__dirname + "/../scripts")) app.use("/accounts", express.static(__dirname + "/../styles")) app.use("/accounts", express.static(__dirname + "/../public")) app.use("/accounts", express.static(__dirname + "/../scripts")) 

这是非常乏味的。 我可以做这样的事吗?

 app.use("*",express.static(__dirname + "/../scripts")) 

注意:我有3个不同目录中的css,js和图像,我需要/thing1/index.css,/thing2/index.css,etc。

谢谢,
阿里

你可以使用这个,这有点hacky国际海事组织,但它应该工作:

 // place this after your routes app.routes.get.forEach(function(r) { app.use(r.path, express.static(__dirname + '/public')); }); 

它只适用于GET路由(因此app.routes.get ),你可能不需要它的每一个路由,所以你可以过滤r.path只挑出你需要的路线)。 另外,我不确定性能的影响。

为什么不直接在网站的根目录下引用静态数据,而不是将它们添加到应用程序URI的每个级别?

这就是大多数人所做的,以及这些框架的devise目的。