javascript node.js 服务器未运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10936214/
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
node.js server not running
提问by CMDadabo
I am trying to learn node.js, but I'm having trouble getting the simple server to run on localhost:8888
.
我正在尝试学习 node.js,但是我无法让简单的服务器在 .js 上运行localhost:8888
。
Here is the code for server.js:
这是 server.js 的代码:
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
server.js runs without errors, and trying netstat -an | grep 8888
from terminal returns
server.js 运行没有错误,并尝试netstat -an | grep 8888
从终端返回
tcp4 0 0 *.8888 *.* LISTEN
However, when I go to localhost:8888 in a browser, it says that it cannot be found.
但是,当我在浏览器中访问 localhost:8888 时,它说找不到它。
I've looked at all the related questions, and nothing has worked so far. I've tried different ports, etc. I know that my router blocks incoming traffic on port 8888, but shouldn't that not matter if I'm trying to access it locally? I've run tomcat servers on this port before, for example. Thanks so much for your help!
我已经查看了所有相关问题,但到目前为止没有任何效果。我尝试过不同的端口等。我知道我的路由器阻止了端口 8888 上的传入流量,但是如果我尝试在本地访问它,这应该无关紧要吗?例如,我以前在这个端口上运行过 tomcat 服务器。非常感谢你的帮助!
node.js version:v0.6.15
OS:Mac OS 10.6.8
node.js 版本:v0.6.15
操作系统:Mac OS 10.6.8
采纳答案by Matthew Ratzloff
This code works--I just tried it and navigated to localhost:8888 and saw the expected output.
这段代码有效——我刚刚尝试过并导航到 localhost:8888 并看到了预期的输出。
It's possible that you have a firewall that's too restrictive or you have a bad install of Node.js.
可能是您的防火墙过于严格,或者您安装的 Node.js 不正确。