Node.js + connect 404 error

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


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 / fail 0 and / fail1 at the same time, one of them will succeed, and the other will fail with 404. call / success0 and success1 however works. Can someone enlighten me why one works and the other doesn't? Below is my client for testing:


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();
}

performed on customer return:

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
+5
source share
1 answer

Here is an explanation of this error (and a correction pointer below).

, Connect ( ) . , , '/fail1', , '/fail0'. , fail0 .

- .

.

+1

All Articles