A Node Problem
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 will hang, and you'll never see "done" because it doesn't drain the `res` Readable Stream. | |
var http = require('http'); | |
var server = http.createServer(function (req, res) { | |
res.writeHead(404); | |
res.end(); | |
}); | |
server.listen(3000, function () { | |
http.get('http://localhost:3000/', function (res) { | |
console.log(res.statusCode); | |
server.close(function () { | |
console.log('done'); | |
}); | |
}); | |
}); |
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 time, you'll see "done" and the process won't hang, because we're draining the | |
// data from the `res` Readable Stream, and the `end` event can then occur. The server | |
// won't send the `end` event if the stream isn't drained. | |
var http = require('http'); | |
var server = http.createServer(function (req, res) { | |
res.writeHead(404); | |
res.end(); | |
}); | |
server.listen(3000, function () { | |
http.get('http://localhost:3000/', function (res) { | |
console.log(res.statusCode); | |
res.on('data', function () {}); | |
res.on('end', function () { | |
server.close(function () { | |
console.log('done'); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One thing I'm still wondering, though: when I tried version 1 (server.js), in Node v.0.11.14, it worked fine. So I guess in the future, the server won't wait for response streams to be drained before closing?