基本 Node.js 示例不适用于 Windows 7

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8553937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:41:22  来源:igfitidea点击:

Basic Node.js examples not working on Windows 7

windowsnode.jswindows-7

提问by CircusNinja

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

我从http://nodejs.org/#downloadv0.6.6安装了 node.js。我正在使用 Windows 7 32 位。

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.

我一直在网上浏览各种 tuts,并想在这样做的同时进行实验,但我似乎无法让 node.js 工作。Node 将运行我的 .js 文件,但来自浏览器的任何请求都会超时。

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

这是一个不起作用的典型 Hello World 示例:

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

将浏览器指向 127.0.0.1:1337 或 localhost:1337 不起作用。来自浏览器的请求超时。我也试过听(1337,'0.0.0.0')和听(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.

我知道服务器正在运行;如果我 CTRL+C 并停止节点,浏览器会立即返回 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.

我也尝试在这个 gist 中运行代码,但它不起作用:https: //gist.github.com/1339846。我最终得到控制台输出“Listening!” 然后没有别的。

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.

我尝试完全禁用防火墙,并且服务停止了。如果我使用 netstat -noa 检查连接,我可以看到节点为浏览器打开了一堆连接,所有连接都处于 CLOSE_WAIT 状态。所以看起来连接正在发生,但 node.js 只是不工作。

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.

应该由请求启动的回调函数永远不会执行——我在各个区域撒了一些 console.log 语句,它们都执行,除了回调中的任何一条。

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.

更新:我几乎要放弃了。我已经尝试了我能想到的所有方法,最终在 VirtualBox 的 Ubuntu 实例中运行 node.js 比抓住稻草更容易。

回答by Brian McGinity

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

!!!!!! 同样的问题发生在我身上......

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

这是我尚未在任何地方找到的解决方案:

Look in Windows Firewall with Advanced Securityand see if Evented I/O for V8 JavaScriptis blocked or appears two times.

进去Windows Firewall with Advanced Security看看Evented I/O for V8 JavaScript是不是被屏蔽了或者出现了两次。

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

如果是这样,取消阻止它并删除重复的条目。如果你安装/卸载/安装 nodeJs,会有 2 个条目。

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 JavaScriptAND IT WILL BE BLOCKED.

此外,当节点首次运行时,窗口防火墙对话框会打开,询问您是否要允许节点访问防火墙。如果您按“否”或直接关闭窗口而不询问,它将创建Evented I/O for V8 JavaScript并且将被阻止。

回答by mhermher

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.

我遇到了同样的问题,在阅读了文档后,我意外地遇到了我认为的解决方案。在我的例子中,我注意到传入的请求被传递到节点,但响应从未触发其“结束”事件。因此,在 Windows 中更改传入的防火墙规则似乎与问题无关。

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.

因此, http.createServer 接受一个参数 - 一个应该包含请求和响应参数的函数。请求参数似乎是问题所在。request 参数是 http.incomingMessage 的一个实例。这个类只有一种事件类型,但它本身也是 Stream.Readable 的一个实现,这是我发现没有触发的“结束”事件的地方。真的没有其他原因,只是为了测试哪个是未触发的第一个事件,我只是为另一种类型的事件(“可读”)添加了一个侦听器,并且只向它添加了一个 console.log 行,这使得整个事情都能正常工作。

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.

在某些情况下,侦听“可读”事件会导致一些数据从底层系统读入内部缓冲区(如果还没有)。

回答by Kiran Rajmohan

  1. Go to "Control Panel\All Control Panel Items\Windows Firewall\Allowed Programs"
  2. Click Allow Programs
  3. select nodejs from the list.
  1. 转到“控制面板\所有控制面板项目\Windows 防火墙\允许的程序”
  2. 单击允许程序
  3. 从列表中选择 nodejs。

This fixed all the problems for me

这为我解决了所有问题

回答by fent

I just tried it and it works for me. Make sure you are not blocking node with your firewall.

我刚刚尝试过,它对我有用。确保您没有使用防火墙阻止节点。

回答by cglave

I am using Windows 7 32-bit.

我正在使用 Windows 7 32 位。

What edition of Windows 7 are you using? Eg. Home Premium, Professional, Ultimate?

您使用的是哪个版本的 Windows 7?例如。家庭高级版、专业版、终极版?

A thread on the npm github projectmentions similar symptoms while installing nodejs modules using npm, and comments seem to narrow it down to being caused by Windows 7 Professional. It being 32/64-bit doesn't seem to matter.

npm github 项目上的一个线程在使用 npm 安装 nodejs 模块时提到了类似的症状,并且评论似乎将其缩小为由 Windows 7 Professional 引起的。它是 32/64 位似乎并不重要。

I am having both the problem you describe, as well as the npm installation problem, and am running on Windows 7 Professional 64-bit.

我遇到了您描述的问题以及 npm 安装问题,并且我在 Windows 7 Professional 64 位上运行。

Using XPMode(a workaround mentioned in the npm thread) has allowed me to workaround both of these issues. Although, I suppose this is just a more Windows-y version of your use of Ubuntu in VirtualBox.

使用XPMode(npm 线程中提到的一种解决方法)让我可以解决这两个问题。虽然,我认为这只是您在 VirtualBox 中使用 Ubuntu 的 Windows-y 版本。

Other workarounds tried without success:

尝试了其他解决方法但没有成功:

  • Make/run a Debug build of v0.6.6
  • Make/run a Debug build of v0.6.5 (actually crashed in startup)
  • Set various Compatability Modes on the installed node.exe
  • Prepackaged Windows installer of v0.6.5
  • 制作/运行 v0.6.6 的调试版本
  • 制作/运行 v0.6.5 的调试版本(实际上在启动时崩溃了)
  • 在安装的 node.exe 上设置各种兼容模式
  • v0.6.5 的预打包 Windows 安装程序

回答by Vini.g.fer

I was having the same problem with this code (Http Server example from this link: http://net.tutsplus.com/tutorials/javascript-ajax/node-js-for-beginners/)

我遇到了与此代码相同的问题(来自此链接的 Http Server 示例:http: //net.tutsplus.com/tutorials/javascript-ajax/node-js-for-beginners/

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

I tried windows 7 64-bit version, windows XP virtual machine, ubuntu virtual machine ... nothing! It only worked after I commented the "request.on" line. Your example (which doesn't have this line) worked fine for me. I'm using the latest stable build from node.js (v0.10.18 for windows or linux). Hope this helps anyone having trouble with this.

我试过windows 7 64位版、windows XP虚拟机、ubuntu虚拟机……什么都没有!它仅在我评论“request.on”行后才起作用。你的例子(没有这条线)对我来说很好。我正在使用 node.js 的最新稳定版本(适用于 Windows 或 linux 的 v0.10.18)。希望这可以帮助任何遇到此问题的人。