mTuran
mTuran

Reputation: 1824

Exclude Some File Types In Express JS Routing

I want to match all /admin/* request expect .png, .gif, .js file types. So write this route:

app.all('/admin*', requiresLogin);

But i can't exclude that file types. How can i exclude .png, .gif, .js request from that routing ?

Upvotes: 0

Views: 1570

Answers (2)

takinola
takinola

Reputation: 1763

You could also just set up express to serve your static files so that requests for .png, .gif, .js etc are already completed before this route

// will serve pictures and scripts
app.use(express.static('path/to/png/gif/and/js/files'));

// will not see requests like '/admin/happy.gif'
app.all('/admin', requireLogin);

Upvotes: 1

nu11p01n73R
nu11p01n73R

Reputation: 26667

How about the regex

\/admin[^.]*\.(?!(?:png|gif|js))

will exculde .png, .gif, .js

see how the regex matches at http://regex101.com/r/mW5qZ9/1

  • \/admin matches /admin

  • [^.]*\. matches anything other than . followed by .

  • (?!(?:png|gif|js)) asserts that the regex is not followed by png|gif|js

Upvotes: 2

Related Questions