Created
August 16, 2012 16:06
-
-
Save zzen/3371383 to your computer and use it in GitHub Desktop.
Node.js HTTP doesn't fire close/end events
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
// This is a testcase for a suspected bug in node.js | |
// server doesn't fire close/end events if the connection was closed in the response before request finished sending data | |
var http = require('http'), util = require('util'), PORT = 8080; | |
// sample HTTP server | |
// responds immediately and closes response | |
var s = http.createServer(function(req, res) { | |
req.on('close', function() { return console.error('!!!REQUEST CLOSED'); }); | |
req.on('end', function() { return console.error('!!!REQUEST ENDED'); }); | |
console.error('server received request'); | |
res.writeHead(200, { 'Content-Type': 'text/plain' }); | |
res.end('Hello World\n'); | |
}).listen(PORT); | |
// sample HTTP client | |
// send first chunk, waits 2s, sends the second chunk and closes connection | |
var req = http.request({ host: '127.0.0.1', port: PORT, method: 'POST', path: '/' }, function(res) { | |
res.on('end', function() { return console.error('response ended'); }); | |
res.on('close', function() { return console.error('response closed'); }); | |
console.error('ciient received response headers'); | |
}); | |
req.write('data0\n'); | |
setTimeout(function() { | |
console.log('sending more data'); | |
req.write('data1\n'); | |
req.end(); | |
}, 2000); | |
/* | |
Actual Output: | |
-------------- | |
> node test.js | |
server received request | |
ciient received response headers | |
response ended | |
[... 2000ms...] | |
sending more data | |
Expected Output: | |
-------------- | |
> node test.js | |
server received request | |
ciient received response headers | |
response ended | |
!!!REQUEST CLOSED | |
[... 2000ms...] | |
sending more data | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment