Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExpressJS: How do I ignore public static files in my route?

app.get("/:name?/:group?", function(req, res){...

is matching files that are in my public directory. So if I include a stylesheet:

<link type="text/css" href="/stylesheets/style.css" />

Node will match /stylesheets/style.css and assign name the value stylesheets and group the value style.css.

What's the best way to avoid this?

like image 576
Luke Burns Avatar asked Jul 16 '11 19:07

Luke Burns


1 Answers

The easiest thing may be to make sure that express runs the static provider middleware prior to the router middleware. You can do this by doing:

app.use(express.static(__dirname + '/public'));
app.use(app.router);

That way the static file will find it and respond and the router won't be executed. I've had similar confusion with the router's default position (last) screwing up with my compilation of coffeescript files. FYI there are docs on this here (search the page for app.router and you'll see an explanatory paragraph.

like image 102
Peter Lyons Avatar answered Nov 09 '22 04:11

Peter Lyons