javascript 为什么 Node.js 应用程序只能从 127.0.0.1/localhost 访问?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22165010/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:35:03  来源:igfitidea点击:

Why is Node.js app accessible only from 127.0.0.1/localhost?

javascriptnode.js

提问by graycrystal

I was about teaching my friend an intro to node but then I wonder why this code from nodejs.org:

我想教我的朋友介绍 node,但后来我想知道为什么这个来自 nodejs.org 的代码:

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

when hosted, it doesn't accessible from public ip (it's still accessible from localhost though) while this code from express.js:

托管时,它无法从公共 IP 访问(尽管它仍然可以从 localhost 访问),而来自 express.js 的此代码:

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 80);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/', routes.index);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

does. Please help me modify the basic code the one from nodejs's homepage so that it become accessible from public ip so I can demonstrate it to my friend in a very basic way. The fresh code generated by express.js worked just fine.

做。请帮我修改nodejs主页上的基本代码,以便它可以从公共IP访问,这样我就可以以一种非常基本的方式向我的朋友展示它。express.js 生成的新代码运行良好。

What am I missing here?

我在这里错过了什么?

回答by thefourtheye

As per the server.listendocs,

根据server.listen文档,

Begin accepting connections on the specified port and hostname. If the hostname is omitted, the server will accept connections directed to any IPv4 address (INADDR_ANY).

开始接受指定端口和主机名上的连接。如果省略主机名,服务器将接受定向到任何 IPv4 地址 (INADDR_ANY) 的连接。

To make it accept connections from all ips (0.0.0.0), change it to read like this

要使其接受来自所有 ips ( 0.0.0.0) 的连接,请将其更改为这样读取

}).listen(80); // No explicit ip, defaults to all ips 0.0.0.0
console.log('Server running in port 80');

回答by CatsDontWearPants

127.0.0.1 is an internal address

127.0.0.1 是内部地址

localhost is the default hostname (local domain) which points to it (see /etc/hosts)

localhost 是指向它的默认主机名(本地域)(请参阅 /etc/hosts)

0.0.0.0 is the address you want to use to make it accessible from the outside (thus make it public)

0.0.0.0 是您要用来使其从外部访问的地址(从而使其公开)

seems like when you do not provide the hostname, node's require('http').createServer().listen() defaults to 0.0.0.0 instead of localhost's 127.0.0.1

似乎当您不提供主机名时,节点的 require('http').createServer().listen() 默认为 0.0.0.0 而不是 localhost 的 127.0.0.1