Express Nodejs: Static files of multiple directories. One of them only allows .png requests

My goal in my NodeJS Express application is to have 2 static directories.

Every file in the first directory (/ client) is available.

Only .png files in the second directory (/ quest) are available.

Here's what I tried to do:

app.use(/\/quest\/.*\.png/,express.static('quest')); //doesnt work

app.use(express.static('client')); //works correctly



//Note: This will actually trigger the messages
app.use(/\/quest\/.*\.png/,function(){
    console.log(100);
});

      

But that doesn't work ...

+3


source to share


2 answers


I'm not really sure why, but the syntax app.use

for some reason only matches the route, it doesn't set the req.url

requested url, which is required express.static

to find the specified file. The syntax is app.VERB

better for handling routes.

Then since you are already telling express.static

to look into the directory /quest

you need to separate that from req.url

or else it will look/quest

/quest/file.png



app.get('/quest/*.png', function(req, res, next) {
    req.url = req.url.replace('/quest','');
    next();
}, express.static('quest'));

      

+2


source


Checked the laggingreflex solution works on express 4.13.0



0


source







All Articles