Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js process.nextTick error

Tags:

node.js

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?

like image 891
davin Avatar asked Mar 13 '11 02:03

davin


People also ask

What is process nextTick () in NodeJS?

Syntax: process.nextTick() Return Value: In the code of snippet, the second console is printed first because this is a part of the current iteration of the event loop, and the first console is a part of a callback function that is associated with the process. nextTick() executed in the next iteration of the event loop.

How do I fix a process out of memory exception in NodeJS?

This exception can be solved by increasing the default memory allocated to our program to the required memory by using the following command. Parameters: SPACE_REQD: Pass the increased memory space (in Megabytes).

What is the difference between process nextTick () and setImmediate ()?

1. process. nextTick() is used to schedule a callback function to be invoked in the next iteration of the Event Loop. setImmediate() method is used to execute a function right after the current event loop finishes.

How do I stop a process from using Stdin?

pause` will "close" `stdin`.


1 Answers

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).

like image 69
davin Avatar answered Sep 25 '22 21:09

davin