5

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
4

1 回答 1

1

这是对这个错误的解释(下面是一个指向修复的指针)。

原因是 Connect 库的路由逻辑将状态(当前路由的索引)作为属性存储在函数回调中。在您的测试用例中,当为第二个路由“/fail1”注册回调时,此路由会覆盖“/fail0”路由设置的状态。因此,fail0 的传入请求失败。

此错误已在此线程的特快邮件列表中报告。

在这个 fork中提交了一个修复程序。

于 2011-03-11T21:50:21.610 回答