sivashanmugam kannan sivashanmugam kannan - 1 year ago 113
Node.js Question

regular expression for a url in node.js express routing

I am working on building node.js server which uses regular expression for routing.

The URL which has to captured in node.js using regex

The people-images-taken-on-
is fixed in the URL. I only wanted to capture month, date and year.

I will capturing the above URL in express like below

app.get('/people-images-taken-on-{only alphabets}-{2 digits}-{4 digits} ',

I found the regex each patterns seperately

allows only alphabets - [a-zA-Z]+$
allows a 4 digits value - (\\d){4}
allows a 2 digits value - (\\d){2}

I tried to combine everything, But i failed miserably many times. Can any one give the correct regex for the url patern.
and also can anyone tell me this regex based path handling is a good way for handling Dates?

Answer Source

You can use named parameters with custom regular expressions, like this:

app.get('/people-images-taken-on-:month([a-zA-Z]+)-:day(\\d{2})-:year(\\d{4})', ...)

This will store the values in req.params like this:

{ month: 'December', day: '05', year: '2011' }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download