Express tests the middleware in order, so if you first put the most specific call to express.static, then it should work, i.e.
app.use(express.static(path.join(__dirname, 'public/images/icons'), { maxAge: 12345 })); app.use(express.static(path.join(__dirname, 'public/images'), { maxAge: 1234567 })); app.use(express.static(path.join(__dirname, 'public/else'), { maxAge: 9874567 })); app.use(express.static(path.join(__dirname, 'public'), { maxAge: 31557600000 }));
and etc.
Edit:
This will not support the path, so I would do
function static(dirname, age) { return express.static(path.join(__dirname, dirname), { maxAge: age }); }
and then call
app.use('/public/images/icons', static('public/images/icons', 12345)); app.use('/public/images/', static('public/images', 1234567);
and etc.
The reason for this is because my previous solutions mount all the static files in the root, whereas this solution mounts every directory in this file path with the correct maxAge
source share