Multiple routes issue with Express + Cradle

97 views
Skip to first unread message

Eran Hammer-Lahav

unread,
Feb 1, 2011, 4:37:30 PM2/1/11
to expre...@googlegroups.com

I am having problems running the following code:


---

var express = require('express');

var cradle = require('cradle');


var connection = new (cradle.Connection)('http://localhost', 5984, { cache: false });

var db = connection.database('test');

var server = express.createServer();


server.get('/1', first, second);

server.get('/2', first, second);


function first(req, res, next) {


    console.log('A: ' + req.url);


    var id = req.url.replace('/', '');


    db.get(id, function (err, doc) {


        console.log('B: ' + req.url + ' / ' + doc._id);

       next();

    });

}


function second(req, res, next) {


    console.log('C: ' + req.url);

    res.send('done');

};


server.listen(8201, "localhost");

console.log('Server started at http://localhost:8201');

---


There is a ‘test’ couchdb database with two documents, one for _id 1 and one for _id 2. This code works fine when requesting /1 or /2, but fails when requesting both in close succession. When asking for /1 and then /2, I get the following printout:


A: /1

A: /2

B: /1 / 1

B: /2 / 2

C: /2


This means the second route is not called for /1. Any ideas?


Thanks,

EHL

Eran Hammer-Lahav

unread,
Feb 1, 2011, 5:44:21 PM2/1/11
to expre...@googlegroups.com
This is not Cradle specific, so I'm starting to think I'm doing something wrong with express...

The same problem happens if you replace function 'first' with this:

function first(req, res, next) {

    console.log('A: ' + req.url);

    var hreq = http.request({ host: 'yahoo.com', port: 80, path: '/', method: 'GET' }, function (hres) {

        console.log('B: ' + req.url);
        next();
    });

    hreq.end();
}

Seems the problem is with calling next() from within the callback.

EHL

Darrell Banks

unread,
Feb 1, 2011, 5:47:10 PM2/1/11
to expre...@googlegroups.com
EHL,

    I don't believe you have access to the next( ) function. Isn't it out of scope where you're calling it?


EHL

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

Eran Hammer-Lahav

unread,
Feb 1, 2011, 5:48:45 PM2/1/11
to expre...@googlegroups.com

It works just fine for a single request. No exceptions or errors. And it does partially work for /2…

 

EHL

Eran Hammer-Lahav

unread,
Feb 1, 2011, 6:29:24 PM2/1/11
to expre...@googlegroups.com
Changing the routes to:

server.get('/1', first);
server.get('/2', first);
server.all('*', second);

Fixes the problem.

EHL

vision media [ Tj Holowaychuk ]

unread,
Feb 1, 2011, 6:44:57 PM2/1/11
to expre...@googlegroups.com
sorry what was the issue?

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

Eran Hammer-Lahav

unread,
Feb 1, 2011, 6:49:41 PM2/1/11
to expre...@googlegroups.com

The issue is that calling next() from within an http request callback sometimes* skips additional routes for that path.

 

* the issue only happens when calling the paths in quick succession (/1, /2, /1, /2) before the server replies back (parallel).

 

For example:

 

var express = require('express');

var http = require('http');

 

var server = express.createServer();

 

server.get('/1', first, second);

server.get('/2', first, second);

 

function first(req, res, next) {

 

    console.log('A: ' + req.url);

 

    var hreq = http.request({ host: 'yahoo.com', port: 80, path: '/', method: 'GET' }, function (hres) {

 

        console.log('B: ' + req.url);

        next();

    });

 

    hreq.end();

}

 

 

function second(req, res, next) {

 

    console.log('C: ' + req.url);

    res.send('done');

}

 

 

server.listen(8201, "localhost");

console.log('Server started at http://localhost:8201');

 

Does *not* work. However, changing the routes to:

 

server.get('/1', first);

server.get('/2', first);

server.all('*', second);

 

Does work.

 

server.get('/1', first);

server.get('/1', second);

server.get('/2', first);

server.get('/2', second);

 

Also doesn’t work.

 

EHL

 

From: expre...@googlegroups.com [mailto:expre...@googlegroups.com] On Behalf Of vision media [ Tj Holowaychuk ]


Sent: Tuesday, February 01, 2011 3:45 PM
To: expre...@googlegroups.com

vision media [ Tj Holowaychuk ]

unread,
Feb 1, 2011, 6:58:03 PM2/1/11
to expre...@googlegroups.com
 I bet I know what that is, this line:


maintains the state on the function, we should change that to req._route_index or something

Eran Hammer-Lahav

unread,
Feb 1, 2011, 7:07:56 PM2/1/11
to expre...@googlegroups.com

Would you like me to fork and submit a pull? Or is this something you can quickly take care of?

 

Happy to do it now (kind of have too… J).

 

Thanks,

vision media [ Tj Holowaychuk ]

unread,
Feb 1, 2011, 7:24:21 PM2/1/11
to expre...@googlegroups.com
I will try and get to it right away here

Eran Hammer-Lahav

unread,
Feb 1, 2011, 7:32:46 PM2/1/11
to expre...@googlegroups.com

I submitted a pull request just in case.

vision media [ Tj Holowaychuk ]

unread,
Feb 1, 2011, 7:53:50 PM2/1/11
to expre...@googlegroups.com
got it :) thanks

Eran Hammer-Lahav

unread,
Feb 1, 2011, 8:37:04 PM2/1/11
to expre...@googlegroups.com

Thanks! Now my day has not been a total waste…

vision media [ Tj Holowaychuk ]

unread,
Feb 1, 2011, 10:02:56 PM2/1/11
to expre...@googlegroups.com
hah :) luckily that was an easy fix, it seems like an obvious one, not sure how it was not an issue before (or in any parallel tests), but whatever, glad it's fixed
Reply all
Reply to author
Forward
0 new messages