Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从node.js访问服务器_Node.js - Fatal编程技术网

从node.js访问服务器

从node.js访问服务器,node.js,Node.js,我有一个js文件,其中包含以下代码 var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(8000, '127.0.0.1'); console.log('Server running'); 现在,如果我以127.0.0.1:

我有一个js文件,其中包含以下代码

var http = require('http');
http.createServer(function (req, res) {
   res.writeHead(200, {'Content-Type': 'text/plain'});
   res.end('Hello World\n');
}).listen(8000, '127.0.0.1');

console.log('Server running');

现在,如果我以127.0.0.1:8000访问go to browser,这很好,但是我想使用服务器的IP访问服务器,但是当我修改服务器IP的代码并执行代码时,会发生错误。有人能解释为什么我无法访问它吗?

您需要省略listen方法的第二个参数,以允许在任何IP上访问:

}).listen(8000);
有关更多详细信息,请参见

如果我按127.0.0.1:8000访问浏览器,这很好,但我想使用IP访问服务器-这正是您要做的,使用主机名将是localhost:8000。我想你需要更准确地解释一下你所说的IP是什么意思。