At the moment I have the states in a string as follows:
var states = 'AL|AK|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL...';
I would like to capture
mydomain.com/CA/more-stuff
Into two separate parameters. The first one with the state information, the second one with the additional. I know how to get the additional information:
app.get('/' ??? + '/:additional', ...);
How can I capture the state information?
If you define it as a parameter in the path...
app.get('/:stateabbr/:additional', ...);
You can validate it with app.param()
:
app.param('stateabbr', function (req, res, next, abbr) {
var stateAbbrs = /AL|AK|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL|../i
if (stateAbbrs.test(abbr)) {
next()
} else {
next(new Error('Unrecognized State abbreviation.'));
}
});
Try this one
var states = 'AL|AK|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL...';
app.get('/:state('+ states + ')/:additional', function(req,res,next){
res.send('state:'+req.params.state+',additional:'+req.params.additional);
})
I've tested it and it should work as You requested :)