简体   繁体   English

远程node.js http服务器无法启动或无法侦听

[英]Remote node.js http server doesnt start or fails to listen

I ran some node.js tests on my windows machine and everything worked well. 我在我的Windows机器上运行了一些node.js测试,一切运行良好。 Now I installed node on my debian remote machine, and Im trying to run a simple http server: 现在我在我的debian远程机器上安装了节点,我试图运行一个简单的http服务器:

var http = require("http");

http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}).listen(8888);

I execute using SSH: node server.js Now when I go to my server ip http://xxx.xxx.22.127:8888 the server times out. 我使用SSH执行: node server.js现在当我转到我的服务器ip http://xxx.xxx.22.127:8888时服务器超时。

What am I doing wrong? 我究竟做错了什么? I tested with some simpler scripts and node.js appears to be installed properly. 我测试了一些更简单的脚本,node.js似乎安装正确。 Can it be a firewall issue, or maybe I should add a host-ip or sommit? 它可能是防火墙问题,或者我应该添加host-ip或sommit?

Side question: when I run node server.js in putty i cant type anymore, how do I return to the commandline? 附带问题:当我在putty中运行node server.js我不能再输入时,如何返回命令行? :) :)

EDIT: My iptables info 编辑:我的iptables信息

Chain INPUT (policy ACCEPT)
target        prot opt source               destination
fail2ban-ssh  tcp  --  anywhere             anywhere            multiport dports ssh
ACCEPT        tcp  --  anywhere             anywhere            tcp dpt:5900
ACCEPT        tcp  --  anywhere             anywhere            tcp dpt:8888

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain fail2ban-ssh (1 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere

There may be a software firewall ( iptables for instance) or a hardware firewall in place which is stopping requests to 8888 from being received. 可能存在软件防火墙(例如iptables )或硬件防火墙,它阻止了对8888请求被接收。 The only way to resolve this is to dig into your server configuration/ network architecture to track down the problem. 解决此问题的唯一方法是深入了解您的服务器配置/网络架构以追踪问题。 For instance, you can use iptables -L if you're using iptables to see what rules are enforced currently. 例如,如果您使用iptables查看当前强制执行的规则,则可以使用iptables -L

There could also be another application already listening on 8888 , but I believe this throws an error rather than failing like this, so I doubt this is the problem. 可能还有另一个应用程序已经在8888监听了,但是我认为这会引发错误而不是像这样失败,所以我怀疑这是问题所在。 One way to check this would be via netstat -a . 检查这个的一种方法是通过netstat -a

Needless to say, I am saying the code you've got should work fine, and it's a problem with your server set-up that is causing this. 毋庸置疑,我说你得到的代码应该可以正常工作,而且你的服务器设置存在问题导致了这个问题。

Side answer: Use nohup and & like so; 侧回答:使用nohup&像这样; nohup node server.js & , although you should look at installing a module such as forever , and then use forever start server.js ; nohup node server.js & ,虽然你应该看看如何forever安装一个模块,然后forever start server.js使用forever start server.js ; forever has the additional benefit of restarting your node process automagically if it crashes. forever有一个额外的好处,如果它崩溃自动重新启动你的节点进程。

What am I doing wrong? 我究竟做错了什么? I tested with some simpler scripts and node.js appears to be installed properly. 我测试了一些更简单的脚本,node.js似乎安装正确。 Can it be a firewall issue, or maybe I should add a host-ip or sommit? 它可能是防火墙问题,或者我应该添加host-ip或sommit?

I see nothing wrong with the script itself. 我认为脚本本身并没有错。 Try to debug it with telnet . 尝试使用telnet进行调试。

Side question: when I run node server.js in putty i cant type anymore, how do I return to the commandline? 附带问题:当我在putty中运行节点server.js我不能再输入时,如何返回命令行? :) :)

node server.js &

(also see man nohup ) (也见man nohup

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM