Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express.js: how to make express.static have higher priority than the rest of the app?

I have an express.js app set up like this:

app.use(express.static(__dirname + '/public'));
...
app.all('*', require('./routes/all'));

So when I try to load /stylesheets/style.css, request is dispatched to the routes. How do I make the app first try to use "static", and then - the catch-all route?

like image 249
Fluffy Avatar asked Jan 12 '12 10:01

Fluffy


People also ask

Is Express GOOD FOR REST API?

Express is a perfect choice for a server when it comes to creating and exposing APIs (e.g. REST API) to communicate as a client with your server application.

Does order of middleware matter Express?

Order matters in middleware because middleware functions are executed in a sequence and you'd need to execute one before the other in order to utilize its functionality.


1 Answers

Middleware get executed in sequential order. Simply put the static middleware before the routing middleware.

app.configure(function() {
  app.use(express.static(__dirname + '/public'));
  app.use(app.router);
});
like image 200
fent Avatar answered Sep 19 '22 16:09

fent