In my code i currently have
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"))
Which is very tedious. can I do something like this?
app.use("*",express.static(__dirname + "/../scripts"))
Note: I have my css,js and images in 3 different directories, and I need /thing1/index.css,/thing2/index.css,etc.
Thanks,
Ari
You could use this, which is a bit hacky IMO but it should work:
// place this after your routes
app.routes.get.forEach(function(r) {
app.use(r.path, express.static(__dirname + '/public'));
});
It only works for GET routes (hence the app.routes.get), and you might not need it for every route so you could filter on r.path to only pick out the routes you need). Also, I'm unsure about performance impact.
Why don't you just reference your statics at the root of your website, instead of adding them to every level of your application URIs?
That is what most people do, and what these frameworks are designed for.