Created
January 25, 2012 07:56
-
-
Save strager/1675280 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Why does this code sample hang client | |
// connections if ASYNC === true? | |
// | |
// # Start server | |
// node test.js --sync port | |
// node test.js --async port | |
// | |
// # Test | |
// curl http://host:port/ | |
var ASYNC = true; | |
var PORT = 8022; | |
var http = require('http'); | |
var i = 2; | |
// --sync, --async flag | |
switch (process.argv[i]) { | |
case '--async': | |
ASYNC = true; | |
++i; | |
break; | |
case '--sync': | |
ASYNC = false; | |
++i; | |
break; | |
default: | |
break; | |
} | |
// Port number | |
if (!isNaN(process.argv[i])) { | |
PORT = Number(process.argv[i]); | |
++i; | |
} | |
http.createServer(function (req, res) { | |
console.log("Request received"); | |
req.on('end', function () { | |
console.log("'end' received"); | |
}); | |
req.pause(); | |
function respond() { | |
req.on('end', function () { | |
// This is not called if ASYNC === true | |
console.log("'end' received (test passed)"); | |
res.writeHead(200); | |
res.end(); | |
}); | |
req.resume(); | |
} | |
if (ASYNC) { | |
process.nextTick(respond); | |
} else { | |
respond(); | |
} | |
}).listen(PORT); | |
console.log( | |
"%s-responding HTTP server listening on :%d", | |
ASYNC ? "Asynchronously" : "Synchronously", | |
PORT | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To test:
curl should return almost instantaneously.