DOTS in route-parameters?

2,155 views
Skip to first unread message

Tronic

unread,
May 20, 2011, 2:34:40 PM5/20/11
to Express
hi,

i have following route:

app.post('/pin/save/:latitude/:longitude', function(req, res) {
...
}

the latitude/langitude values have the following format:

e.g. 43.273844

the problem is, when passing the params, I get an error 500, when I
remove the dots it works perfectly...

any advice how I can achieve, that the route can use values containing
dots?

thanks in advance!

Joshua Cohen

unread,
May 20, 2011, 2:54:28 PM5/20/11
to expre...@googlegroups.com
You can use a regex to specify the path components (something like -?\d+\.\d+). Take a look at http://expressjs.com/guide.html#routing.

That being said, seems like this might potentially be a bug in the router? It should probably ignore dots that are part of path parameters? I haven't delved as much into that code as others have, so I can't say how difficult that would be ;).


--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.


vision media [ Tj Holowaychuk ]

unread,
May 20, 2011, 3:22:26 PM5/20/11
to expre...@googlegroups.com
hmm dots shouuuuld be fine

--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.




--
Tj Holowaychuk
Vision Media
President & Creative Lead

Tronic

unread,
May 20, 2011, 6:14:14 PM5/20/11
to Express
okay, and how can i use regex combined with variables?

like this? app.get('/foo/:bar(...regex...)',....)

?? thanks

On May 20, 8:54 pm, Joshua Cohen <defea...@gmail.com> wrote:
> You can use a regex to specify the path components (something like
> -?\d+\.\d+). Take a look athttp://expressjs.com/guide.html#routing.

Tronic

unread,
May 20, 2011, 6:15:19 PM5/20/11
to Express
so should i write a bug report? i use express 2.3.6

On May 20, 9:22 pm, "vision media [ Tj Holowaychuk ]" <t...@vision-

TJ Holowaychuk

unread,
May 20, 2011, 6:15:58 PM5/20/11
to expre...@googlegroups.com
  app.get('/foo/:bar(\\d+)', ...) etc, just remember it's a string so you need to escape the escape char

-- 
TJ Holowaychuk

Tronic

unread,
May 20, 2011, 6:41:03 PM5/20/11
to Express
okay, i tried the following:

app.post('/pin/save/:latitude(\\d+\.\d+)/:longitude(\\d+\.\d+)',
function(req, res) {

doesn't work. i get 404 now :/ maybe a typo?

thanks

TJ Holowaychuk

unread,
May 20, 2011, 6:49:19 PM5/20/11
to expre...@googlegroups.com


    app.post('/pin/save/:lat(\\d+.\\d+)/:long(\\d+.\\d+)', function(req, res){
      res.send(req.params.lat + ' ' + req.params.long);
    });

    app.post('/pin/save2/:lat([0-9]+.[0-9]+)/:long([0-9]+.[0-9]+)', function(req, res){
      res.send(req.params.lat + ' ' + req.params.long);
    });


both will work, as well as some other alternatives, and of course a regular expression. it's a bit confusing with the escapes, and "." is always literal in the routes so express adds \. for you. i need more docs for this haha

-- 
TJ Holowaychuk

Tronic

unread,
May 20, 2011, 7:03:33 PM5/20/11
to Express
:/ tried but no luck ... 500

app.post('/pin/save/:latitude(\\w+.\\w+)/:longitude(\\w+.\\w+)',
function(req, res) {

TJ Holowaychuk

unread,
May 20, 2011, 7:05:47 PM5/20/11
to expre...@googlegroups.com
personally i think the regexps have limited use, unless you use the same route for multiple segments based on say digits, \w+ etc, but it's not replacement for real validation. should work though i just tried the ones i showed you

-- 
TJ Holowaychuk

Tronic

unread,
May 20, 2011, 7:15:16 PM5/20/11
to Express
okay, i got it, the problem was the ajax request i made to this
route.... sorry!

thank you!

TJ Holowaychuk

unread,
May 20, 2011, 7:16:42 PM5/20/11
to expre...@googlegroups.com
I'd like to have this too:

   app.param('lat', '\\d+.\\d+') etc

   app.get('/whatever/:lat', ...)


-- 
TJ Holowaychuk

Tronic

unread,
May 20, 2011, 7:30:41 PM5/20/11
to Express
thank you. great work an express so far, congrats. looking forward to
the next releases!

Christoffer Davidsson

unread,
Jun 2, 2011, 5:11:21 AM6/2/11
to Express
I have a (possibly) related issue.

I am trying to create a web service mock. Users should be able to
specify a URL-ending and a response.
Then calling SERVER/user/12313/mock/{URL-ending} should respond with
whatever response was specified.

So I tried with:
app.get('/user/:username/mock/:mock_url(*)', function(req, res){}

Dots (.) work fine. I get mock_urls like "/long/12.12/lat/12.12".
However, questions marks (?) do not work.

I suspected for a second question marks would render parameters
available in req.params.key = value for /blah?key=value
Which would have been fint, but they don't.

Any ideas?

Laurie Harper

unread,
Jun 2, 2011, 10:18:26 AM6/2/11
to expre...@googlegroups.com
Routes match the request path, not the query parameters; to get at
the query parameters (the name=value pairs following ?), use req.param
() (see here: http://expressjs.com/guide.html#req.param%28%29).

L.

>>>>> +unsub...@googlegroups.com.


>>>>> For more options, visit this group athttp://groups.google.com/
>>>>> group/express-js?hl=en.
>>

>>> --
>>> You received this message because you are subscribed to the
>>> Google Groups "Express" group.
>>> To post to this group, send email to expre...@googlegroups.com.
>>> To unsubscribe from this group, send email to express-js

>>> +unsub...@googlegroups.com.


>>> For more options, visit this group athttp://groups.google.com/
>>> group/express-js?hl=en.
>

> --
> You received this message because you are subscribed to the Google
> Groups "Express" group.
> To post to this group, send email to expre...@googlegroups.com.
> To unsubscribe from this group, send email to express-js

> +unsub...@googlegroups.com.


> For more options, visit this group at http://groups.google.com/
> group/express-js?hl=en.
>

--
Laurie Harper
Open Source advocate, Java geek: http://www.holoweb.net/laurie
Founder, Zotech Software: http://www.zotechsoftware.com/

Joshua Cohen

unread,
Jun 2, 2011, 10:26:20 AM6/2/11
to expre...@googlegroups.com
On a somewhat-related note, I found out yesterday that request.params (i.e. the property that's stuffed on to the request object by the router middleware, not the function mentioned by Laurie below) *only* contains the matched path parameters, and not the parsed query string parameters. What do folks think about a patch that would add the query params to this property?

On Thu, Jun 2, 2011 at 9:18 AM, Laurie Harper <lau...@holoweb.net> wrote:
Routes match the request path, not the query parameters; to get at the query parameters (the name=value pairs following ?), use req.param() (see here: http://expressjs.com/guide.html#req.param%28%29).

L.
app.post('/pin/save/:lat(\\d+.\\d+)/:long(\\d+.\\d+)', function(req, res){
res.send(req.params.lat + ' ' + req.params.long);

});

app.post('/pin/save2/:lat([0-9]+.[0-9]+)/:long([0-9]+.[0-9]+)', function(req, res){
res.send(req.params.lat + ' ' + req.params.long);

});

both will work, as well as some other alternatives, and of course a regular expression. it's a bit confusing with the escapes, and "." is always literal in the routes so express adds \. for you. i need more docs for this haha
--
TJ Holowaychuk

On Friday, May 20, 2011 at 3:41 PM, Tronic wrote:
app.post('/pin/save/:latitude(\\d+\.\d+)/:longitude(\\d+\.\d+)',
function(req, res) {

--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.

For more options, visit this group athttp://groups.google.com/group/express-js?hl=en.
--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.

For more options, visit this group athttp://groups.google.com/group/express-js?hl=en.
--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/express-js?hl=en.

--
Laurie Harper
Open Source advocate, Java geek: http://www.holoweb.net/laurie
Founder, Zotech Software: http://www.zotechsoftware.com/
--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.

Christoffer Davidsson

unread,
Jun 2, 2011, 11:23:57 AM6/2/11
to Express
Laurie: All right, then I know what heppens to 'em. It would still
require some hacking though to make it work for me. Would you expect
(*) to include every character (part of the query parameters or not)?
> > For more options, visit this group athttp://groups.google.com/

TJ Holowaychuk

unread,
Jun 2, 2011, 11:27:05 AM6/2/11
to expre...@googlegroups.com
req.query is the querystring, req.params is from the route segments, and req.body is from bodyParser if used. req.param() checks all three

-- 
TJ Holowaychuk
To unsubscribe from this group, send email to express-js+...@googlegroups.com.

Laurie Harper

unread,
Jun 2, 2011, 1:07:53 PM6/2/11
to expre...@googlegroups.com
req.param() already covers route params, request query part params and body data params; see here: http://expressjs.com/guide.html#req.param%28%29

L.

Joshua Cohen

unread,
Jun 2, 2011, 1:25:24 PM6/2/11
to expre...@googlegroups.com
Right, but I'm talking about the request.params property, not the request.param function. I've got some code where I need to pass *all* of the params for a given route off to another function, so I end up having to loop over either request.params or request.query to merge the two into a single object (it's only handling GETs, so no need to deal with request.body for now). It just seemed weird to me that request.param("foo") would return a value from the query string, but request.params would not actually contain that value.

Maybe it's just an issue with naming confusion, if request.params is just meant to contain path parameters, maybe it should be named to reflect this (ignoring the compatibility issues that would raise for the sake of discussion ;)).

Laurie Harper

unread,
Jun 2, 2011, 2:00:33 PM6/2/11
to expre...@googlegroups.com
Oops, sorry; yes, you're right, the semantics of .params and .param() are different. That *can* be confusing, especially when new to Express. AFAIK, though, .params is undocumented and so, presumably, not part of the official API. If it were renamed, what would it make sense to rename it to?

L.

TJ Holowaychuk

unread,
Jun 2, 2011, 2:03:11 PM6/2/11
to expre...@googlegroups.com
it could be req.path.foo etc, though that doesn't make much sense either IMO. I never use req.param() personally, though people seem to jump to that before accessing any of the properties directly

-- 
TJ Holowaychuk

Laurie Harper

unread,
Jun 2, 2011, 2:18:50 PM6/2/11
to expre...@googlegroups.com
Probably because req.params and req.query aren't documented other than a mention under req.param() ;-)

Joshua Cohen

unread,
Jun 2, 2011, 2:25:09 PM6/2/11
to expre...@googlegroups.com
req.params is documented: http://expressjs.com/guide.html#routing

So, I'm not really sure that renaming it right now is the best option, since it won't be backwards compatible. Like I said, I'd opt for just merging req.params, req.query, req.body as part of req.params. This would have to follow the same rules as req.param(), which is to say, route/path params take precedence in the event of collisions, then query params, then body.

TJ Holowaychuk

unread,
Jun 2, 2011, 2:26:52 PM6/2/11
to expre...@googlegroups.com
I definitely don't want to merge them, but renamed is as simples as req.path = req.params etc

-- 
TJ Holowaychuk
Reply all
Reply to author
Forward
0 new messages