Reputation: 45545
I'm experiencing something strange with node.js:
When I try and use an http client with only the following code:
require('http').get({host:'127.0.0.1',port:9000, path:'/'}, function(res){
var data = '';
res.setEncoding('utf8');
res.on('data', function(chunk){
data += chunk;
});
res.on('end', function(){
console.log(data);
});
});
An error is thrown:
node.js:116
throw e; // process.nextTick error, or 'error' event on first tick
^ TypeError: Cannot call method 'emit' of undefined
at Socket.<anonymous> (http.js:1174:9)
at Socket.emit (events.js:42:17)
at Array.<anonymous> (net.js:799:27)
at EventEmitter._tickCallback (node.js:108:26)
When I browse to 127.0.0.1:9000 in my browser I get the desired webpage. Furthermore, in the web host logs I can see that there has been a successful connection (something that doesn't happen if I use, say, localhost
instead of 127.0.0.1
. Just an aside).
I say it's funny because if I change the host to google or whatnot everything works fine and it spits out the html to the console.
I should note, I'm running node 0.4.2 under cygwin, built from source.
Anyone seen/dealt with this before?
Upvotes: 16
Views: 12575
Reputation: 5529
you can update the node using
$ sudo n stable
I have this problem with commonplace and get resolved by updating the node
Upvotes: 1
Reputation: 45545
I'm pretty sure I've figured out what causes this error:
When there is no
Content-Length
HTTP header.
There are circumstances when this header is ignored, specifically when Transfer-Encoding: chunked
is set, and then node plays nice. However for basic gets with node, transfer encoding will often not be set to chunked because the hosting server isn't streaming stuff, but they often don't set a Content-Length
header (whether or not this is standard-compliance is questionable, the standard says that unless prohibited by other rules this header SHOULD be set, although these are real world cases which should be dealt with in any case).
I checked this with my own localhost, and when I set a Content-Length
, node suddenly plays nice.
As @dhofstet mentioned in the comments (and in the issue), he found a url that also breaks node: www.cnbc.com/id/41863659/
When he posted this, I checked, and it did indeed break the http.get()
.
I rechecked now, and at the time of writing the url now does issue a Content-Length
header, and miracle of miracles, node no longer crashes on that url.
Would be nice if someone could confirm this (shouldn't take too long to setup a localhost server if you don't already have one, and dish out a basic response with custom headers).
Upvotes: 8