I am writing a nodejs application. I want to to route all the requests to some common API. For that I am using
app.get('*', function(req, res) {
/* Do Something */
});
In "Do Something" Section I want to retrive the req.route.regexp and then based on that I will do processing. I am expecting req.route.regexp should be the exact URL's regexp for which we came here. But as I am handling this in '*', so regular expression is not giving me equivalent req.route.params.
So is there any way to get the regexp based on req.route.params in app.get of all routes (*).
Ex: I have url as: "/:val", for that I am expecting regexp "/^/(?:([^/]+?))/?$/i", but I am getting regexp for "*", so how I can achieve this?
When the handler of app.get('*', ...) is called, the eventual route hasn't been matched yet and express cannot possibly tell you about a subsequent route.
The only solution that I see would be to manually call a handler in all your routes:
app.get('/:val', function (req, res) {
if(!myGlobalHandler(req, res))
return;
});
function myGlobalHandler(req, res) {
if(somethingWrong(req)) {
res.send({error: 'uh oh...'});
return false;
}
return true;
}
If you tell me more about your use-case, maybe there's a more elegant solution.