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
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.
It works just fine for a single request. No exceptions or errors. And it does partially work for /2…
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.
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
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,
I submitted a pull request just in case.
Thanks! Now my day has not been a total waste…