I am using expressJs 3.x and node 0.8.8. In my application I tried to catch all invalid urls. So I have added the following code. It is working fine, it is also catching the url, if I try to access the public or static files.
app.all('/*', function(req, res){
\\ Do some action like redirecting or whatever ...
});
How to avoid or pass static folder files (GET Request) and how to catch only invalid urls ?
Most likely, you're calling app.use(app.router)
prior to app.use(express.static(...))
, so that express
tries to find matching route first, and only if there is no matched route it tries to find the corresponding static files.
The configuration code should be like that:
app.configure(function () {
app.use(express.static(__dirname + '/public'));
app.use(app.router);
});
so that express
will try to find a static file first.
There are many reasons Express
does not differentiate between statics and non-statics, this may be a gotcha at times but it is extremely flexible.
I'm not sure what you mean by expressjs have to differentiate static calls
, there's no such thing as an invalid url
, it's just how you interpret an arbitrary string.