我正在尝试使用NodeJS在我的Web服务器上运行一个简单的JS脚本:
var http = require('http');
var server = http.createServer(function(req,res) {
res.writeHead(200,{"Content-Type":"text/plain"});
res.end("Hello World!");
});
server.listen(8000);
console.log('Server running');
我在本地运行时的输出:
curl http://127.0.0.1:8000
Hello World!
但是,当我尝试卷曲到我的网络服务器时,它会超时。 我不知道它是否重要但我在Web服务器上安装了apache2。非常感谢任何帮助。
答案 0 :(得分:1)
您无法从外部访问node.js,因为它正在侦听localhost IP,即127.0.0.1。您需要配置node.js以侦听0.0.0.0,以便它能够接受计算机所有IP上的连接。 试试这个:
server.listen(8000, "0.0.0.0");
如果它仍然不起作用并且您正在使用iptables,则可能需要打开端口8000.试试这个:
iptables -I INPUT -p tcp --dport 8000 -j ACCEPT