wildcat * route potential bug

13 views
Skip to first unread message

Austin Chau

unread,
Apr 23, 2011, 2:50:07 AM4/23/11
to Express
Hi,

I am running exp...@2.2.2 and these are the routes that i used which
produce a behavior that was unexpected

var foo = function(req, res, next) {
console.log('foo');
next();
};

app.get('/test', function(req, res, next) {
res.send('test');
});

app.get('*', foo,function(req, res, next) {
res.send('should not get here on /test');
});

Okay, since the route '/test' appears before '*', so I expected that '/
test' will be chosen and invoked. But it turns out that the "*" is
chosen first, which hit the foo() method and then the next() will
choose '/test'. Am I doing something wrong or misunderstanding how
routes are selected by the framework? Thanks for your time

vision media [ Tj Holowaychuk ]

unread,
Apr 23, 2011, 5:01:53 PM4/23/11
to expre...@googlegroups.com
works as expected for me, GET /test responds "test" and GET /testing responds with the other


--
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

Austin Chau

unread,
Apr 23, 2011, 5:18:51 PM4/23/11
to Express
TJ,

Yes I know it responds with "test" but if you look at console log
"foo" is also printed out. So it seems that the "*" route is executed
first and the next() send it to the "/test" route. Do you not see the
same behavior?

On Apr 23, 2:01 pm, "vision media [ Tj Holowaychuk ]" <t...@vision-
media.ca> wrote:
> works as expected for me, GET /test responds "test" and GET /testing
> responds with the other
>
>
>
>
>
>
>
>
>
> On Fri, Apr 22, 2011 at 11:50 PM, Austin Chau <austin.c...@gmail.com> wrote:
> > Hi,
>
> > I am running expr...@2.2.2 and these are the routes that i used which

Laurie Harper

unread,
Apr 23, 2011, 6:48:23 PM4/23/11
to expre...@googlegroups.com
Add more logging.

What's actually happening is that both routes match /test, so both get fired. The /test route is registered first, so it fires first, generating the 'test' response. The /* route is registered later, so it fires later. Its call to res.send() has no effect, because a response has already been sent.

L.

--
Laurie Harper
http://laurie.holoweb.net/

Austin Chau

unread,
Apr 23, 2011, 9:08:58 PM4/23/11
to Express
Hi Laurie,

thanks for the tips!

I thought expressjs only pick one route to match an incoming request
or maybe this is an exceptional case because it is the single wildcard
"*" route?

I mean if I have two routes "/test" and "/tes*" in that order, a
request coming in /test will ONLY match the "/test" but not the "/
tes*", right? In this case the "/tes*".

Thank you so much for clarifying.

A

vision media [ Tj Holowaychuk ]

unread,
Apr 23, 2011, 10:18:28 PM4/23/11
to expre...@googlegroups.com
they are invoked in order of definition. routes defined after *can* still be matched, but only if the previously matched routes next(). I don't get "foo" for GET /test though however.

Austin Chau

unread,
Apr 24, 2011, 1:11:05 AM4/24/11
to Express
TJ,

I am still able to reproduce it with this exactly script. hit this
server with /test and it will render 'test' but you will also see
'foo' on the console. I am running latest stable exp...@2.2.2 and
con...@1.3.0

var express = require('express');

var app = express.createServer();

app.get('/test', function(req, res) {
res.send('test');
});

app.get('*', function(req, res) {
console.log('foo');
});

app.listen('8888');



On Apr 23, 7:18 pm, "vision media [ Tj Holowaychuk ]" <t...@vision-

vision media [ Tj Holowaychuk ]

unread,
Apr 24, 2011, 12:37:37 PM4/24/11
to expre...@googlegroups.com
weird man :s im using 2.2.2 as well and I dont get this with that exact script as well

Tom

unread,
Apr 24, 2011, 12:39:44 PM4/24/11
to expre...@googlegroups.com
If that is true the only difference I could think of would be the production environment setting, could that be related?

2011/4/24 vision media [ Tj Holowaychuk ] <t...@vision-media.ca>

vision media [ Tj Holowaychuk ]

unread,
Apr 24, 2011, 12:43:21 PM4/24/11
to expre...@googlegroups.com
shouldn't affect anything

Laurie Harper

unread,
Apr 24, 2011, 9:07:52 PM4/24/11
to expre...@googlegroups.com
Modify the sample code to log the requested url in both handlers:

var express = require('express');
var app = express.createServer();

app.get('/test', function(req, res) {
 console.log('bar '+req.url);
 res.send('test');
});

app.get('*', function(req, res) {
 console.log('foo '+req.url);
});

app.listen('8888');

If you run 'curl http://localhost:8888/test' you'll see 'bar /test'. If you hit the URL with a browser, you will probably see 'bar /test' followed by 'foo /favicon.ico'.

Like I said before, add more logging :-)

L. 
-- 
Laurie Harper



Austin Chau

unread,
Apr 25, 2011, 3:40:54 AM4/25/11
to Express
Oh that made sense, sorry I didn't think more what you meant by more
logging. Appreciate the help!

On Apr 24, 6:07 pm, Laurie Harper <lau...@holoweb.net> wrote:
> Modify the sample code to log the requested url in both handlers:
>
> var express = require('express');
> var app = express.createServer();
>
> app.get('/test', function(req, res) {
>  console.log('bar '+req.url);
>  res.send('test');
>
> });
>
> app.get('*', function(req, res) {
>  console.log('foo '+req.url);
>
> });
>
> app.listen('8888');
>
> If you run 'curlhttp://localhost:8888/test'you'll see 'bar /test'. If you hit the URL with a browser, you will probably see 'bar /test' followed by 'foo /favicon.ico'.
>
> Like I said before, add more logging :-)
>
> L.
>
> On 2011-04-24, at 12:43 PM, vision media [ Tj Holowaychuk ] wrote:
>
>
>
>
>
>
>
>
>
> > shouldn't affect anything
>
> > On Sun, Apr 24, 2011 at 9:39 AM, Tom <tommed...@gmail.com> wrote:
> > If that is true the only difference I could think of would be the production environment setting, could that be related?
>
> > 2011/4/24 vision media [ Tj Holowaychuk ] <t...@vision-media.ca>
> > weird man :s im using 2.2.2 as well and I dont get this with that exact script as well
>
> > On Sat, Apr 23, 2011 at 10:11 PM, Austin Chau <austin.c...@gmail.com> wrote:
> > TJ,
>
> > I am still able to reproduce it with this exactly script.  hit this
> > server with /test and it will render 'test' but you will also see
> > 'foo' on the console.  I am running latest stable expr...@2.2.2 and
> > conn...@1.3.0
> > For more options, visit this group athttp://groups.google.com/group/express-js?hl=en.
>
> > --
> > Tj Holowaychuk
> > Vision Media
> > President & Creative Lead
>
> > --
> > 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.
>
> > --
> > Tj Holowaychuk
> > Vision Media
> > President & Creative Lead
>
> > --
> > 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.

vision media [ Tj Holowaychuk ]

unread,
Apr 25, 2011, 11:58:01 AM4/25/11
to expre...@googlegroups.com
ahahaha damn favicon! that would explain what he was talking about. when in doubt just curl :D

Laurie Harper

unread,
Apr 25, 2011, 4:33:08 PM4/25/11
to expre...@googlegroups.com
Yep, never trust a browser to do only what you ask of it... ;-)
-- 
Laurie Harper



Reply all
Reply to author
Forward
0 new messages