Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js + connect 404 error

I just started learning node.js. I have following (server) sample:


var app = require("express").createServer();
app.listen(80);

function fail(req, res, next) { setTimeout(next, 10); }
function success() {
    return function(req, res, next) { setTimeout(next, 10); };
}
app.get("/success0", success(), function(req, res, next) { res.send("0"); });
app.get("/success1", success(), function(req, res, next) { res.send("1"); });
app.get("/fail0", fail, function(req, res, next) { res.send("0"); });
app.get("/fail1", fail, function(req, res, next) { res.send("1"); });

If I call /fail0 and /fail1 simultaneously, one of them will succeed and the other one fails with 404 error. calling /success0 and success1 however works. Can someone enlighten me why one works and the other doesn't? Below is my testing client:


var http = require("http");
var sys = require("sys");

for(var i = 0; i < 10; i++) {
    var io = http.createClient(80, "localhost");
    var request = io.request("GET", "/fail" + (i%2), {host:"localhost"});
    request.on("response", function(response) {
        var body = "";
        response.on("data", function(data) { body += data; });
        response.on("end", function() {
            sys.puts(response.statusCode + ":" + body);
        });
    }).end();
}

running above client returns:

404:Cannot GET /fail0
200:1
404:Cannot GET /fail0
200:1
404:Cannot GET /fail0
200:1
404:Cannot GET /fail0
200:1
404:Cannot GET /fail0
200:1
like image 523
Gabe Avatar asked Nov 15 '22 06:11

Gabe


1 Answers

Here is an explanation of this bug (and a pointer to a fix follows below).

The reason is that the routing logic of the Connect library stores state (the current route's index) as a property in the function callback. In your test case, when the callback is registered for the second route, '/fail1', this one overrides the state set by the route for '/fail0'. An incoming request for fail0 therefore fails.

This bug was reported on the express mailing list in this thread.

A fix was committed in this fork.

like image 134
alienhard Avatar answered Dec 20 '22 14:12

alienhard