Skip to content

Instantly share code, notes, and snippets.

@andrew8088
Last active August 29, 2015 14:08
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save andrew8088/7614acd61329a634bd7c to your computer and use it in GitHub Desktop.
A Node Problem
// 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 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');
});
});
});
});
@andrew8088
Copy link
Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment