我刚刚在远程共享服务器上安装了node.js 0.8.2(我无法获得更新的版本)。到目前为止,我能够在运行node file.js
后登录到控制台。但是,我希望能够让服务器正常工作(显然),但它无法正常工作。这是我试图运行的代码:
//run with node.js
console.log('hello working thingy');
var http=require('http');
var server=http.createServer(function(req,res){
console.log('requested');
res.writeHead(200,{'Content-Type':'text/plain'});
res.end('Hello World!');
});
server.listen(1234,'127.0.0.1');
当我在putty中运行时它会显示hello working thingy
,但是当我转到mydomain.com:1234
时,浏览器中没有任何操作,并且requested
未记录到控制台。我还尝试用'127.0.0.1
替换mydomain.com
,并完全抛弃该论点。我该怎么做才能让它发挥作用?
答案 0 :(得分:1)
server.listen(1234, '0.0.0.0');
就是您想要的。 127.0.0.1
是环回地址,仅用于同一计算机本地连接。 0.0.0.0
表示"侦听所有IP地址"。