I would like to know if it is possible to check a specific format (a Regex expression) of a Express.js URL query, in the query itself (without entring the callback.)
Concretely, I would like to perform a different action, depending on if the query URL is a string or a number (like a user id and user name):
app.get('/:int', function(req, res){
// Get user info based on the user id.
}
app.get('/:string', function(req, res){
// Get user info based on the user name.
}
Can I filter numbers in the first parameter of the app.get
, or is it impossible except doing the test inside the callback:
/(\d)+/.test(req.params.int)
/(\w)+/.test(req.params.string)
You can specify a pattern for a named parameter with parenthesis:
app.get('/:int(\\d+)', function(req, res){
// Get user info based on the user id.
}
app.get('/:string(\\w+)', function(req, res){
// Get user info based on the user name.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With