Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic Node.js examples not working on Windows 7

I installed node.js from http://nodejs.org/#download, v0.6.6. I am using Windows 7 32-bit.

I've been going through various tuts online, and want to experiment while doing so, but I cannot seem to get node.js working. Node will run my .js file, but any request from the browser times out.

Here is a typical Hello World example that does not work:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337);

Pointing my browser at 127.0.0.1:1337 or localhost:1337 does not work. The request from the browser times out. I've also tried listen(1337,'0.0.0.0') and listen(1337,'127.0.0.1').

I know the server is running; if I CTRL+C and stop node, the browser immediately comes back with ERR_CONNECTION_RESET.

I also tried running the code in this gist, which will not work: https://gist.github.com/1339846. I end up with the console output "Listening!" and then nothing else.

Furthermore, I have tried different ports, and my firewall is off via

netsh firewall set opmode mode=disable

I tried with firewall totally disabled, and the service stopped. If I check connections using netstat -noa, I can see node has a bunch of connections opened for the browsers, all in state CLOSE_WAIT. So it looks like connections are happening, but node.js just isn't working.

The callback function that is supposed to be initiated by a request never executes - I sprinkled some console.log statements in various areas, and they all execute except any in the callback.

I uninstalled, re-installed, tried a couple previous builds, restarted my machine...nothing.

Any help is appreciated!

UPDATE: I have just about given up. I've tried everything I can think of, and it ended up being easier to run node.js in an instance of Ubuntu in VirtualBox than grasp at straws.

like image 529
CircusNinja Avatar asked Dec 18 '11 18:12

CircusNinja


People also ask

Will NodeJS work on Windows 7?

Normally old operating system needs an old version of Node. JS. you can refer to old version of Node. JS here, you can also download this one, which is tested and working fine with Win7( win7 Ultimate v6.

Which version of node JS works on Windows 7?

It installs v13. 14.0 LTS. It seems that v13. 14.0 LTS is the last installer that works on Window 7.

Why node is not working?

Make sure the node path is added, if not added it. After doing this restart Visual Studio or open a fresh command prompt. From the command prompt type 'node -v' to echo the node version installed. You can also add the path to node or any other application directly on the command line.

How do I run a simple node script?

The usual way to run a Node. js program is to run the globally available node command (once you install Node. js) and pass the name of the file you want to execute. While running the command, make sure you are in the same directory which contains the app.


2 Answers

!!!!!! Same problem happened for me....

Here is a solution which I have yet to find anywhere:

Look in Windows Firewall with Advanced Security and see if Evented I/O for V8 JavaScript is blocked or appears two times.

If so unblock it and delete the duplicated entry. If you install/uninstall/install nodeJs, there will be 2 entries.

Also when node first runs the Window Firewall dialog opens asking if you want to allow node to have firewall access. If you press "No" or just close the window without asking, it will create Evented I/O for V8 JavaScript AND IT WILL BE BLOCKED.

like image 149
Brian McGinity Avatar answered Oct 12 '22 11:10

Brian McGinity


I ran into the same problem and after reading through the documentation, I unexpectedly ran into what I believe is the solution. In my instance I was noticing that the incoming requests WERE being delivered to node, but the response was never having its 'end' event triggered. So altering incoming firewall rules in windows did not seem to be related to the problem.

So, http.createServer takes in a single argument - a function which should include a request and response parameter. The request parameter seemed to be where the problem lay. The request parameter is an instance of http.incomingMessage. This class only had like one event type, but it was itself also an implementation of Stream.Readable, which is where I found the 'end' event that wasn't triggered. Really for no other reason that to just test which was the first event not triggered, I just added a listener for another type of event ('readable'), and only added a console.log line to it which made the whole thing work.

So the code looks simply something like this:

var http = require("http");
http.createServer(function (request, response) {
    console.log('request');
    request.on('readable', function(){
        console.log('request readable');
    });
    request.on("end", function () {
        console.log('request end');
        response.writeHead(200, {
            'Content-Type': 'text/plain'
        });
        response.end('Hello HTTP!');
    });
}).listen(8080);

The above code works, whereas the earlier version below without a 'readable' event listener does not ever respond:

var http = require("http");
http.createServer(function (request, response) {
    console.log('request');
    request.on("end", function () {
        console.log('request end');
        response.writeHead(200, {
            'Content-Type': 'text/plain'
        });
        response.end('Hello HTTP!');
    });
}).listen(8080);

I am not sure why this works except for a little clue in the documentation which reads:

In some cases, listening for a 'readable' event will cause some data to be read into the internal buffer from the underlying system, if it hadn't already.

like image 30
mhermher Avatar answered Oct 12 '22 10:10

mhermher