为什么当主机为0

为什么当主机为0

本文介绍了为什么当主机为0.0.0.0时,浏览器为何尝试通过https获取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由 create-react-app 创建的应用.构建后,它将生成带有一些JS和CSS文件的html文件.html文件看起来像

 <!doctype html> ...<脚本src ="/static/js/main.89f33fbb.chunk.js"</script/</body></html> 

我尝试通过Express.js服务器托管那些静态文件.

  const app = express().use(bodyParser.json()).use(express.static(path.join(__ dirname,'../dist')))app.listen(5000,'0.0.0.0'); 

当我尝试打开

但是,当我尝试打开

我知道< script src ="/static/js/main.89f33fbb.chunk.js" 会尝试通过 https 获取文件首先,但如果不存在,我希望它能够通过 http .

我在私有模式下尝试使用Chrome,Firefox和Safari,结果相同.

我做错了什么地方吗?

解决方案

您可以使用0.0.0.0侦听任何地址,但是您不应使用它发送请求,因为0.0.0.0是不可路由的元,用于指定无效,未知或不适用目标的地址.

在服务器的上下文中,0.0.0.0表示本地计算机上的所有IPv4地址".如果主机有两个IP地址192.168.1.1和10.1.2.1,并且主机上运行的服务器侦听0.0.0.0,则这两个IP都可以访问,但是如果要访问服务器,则应使用其中一个IP.

看看:

I have an app created by create-react-app.After building, it generates a html file with some JS and CSS files.The html file looks like

<!doctype html>...<script src="/static/js/main.89f33fbb.chunk.js"></script></body></html>

I try to host those static files by an Express.js server.

const app = express()
  .use(bodyParser.json())
  .use(express.static(path.join(__dirname, '../dist')))

app.listen(5000, '0.0.0.0');

When I try to open http://127.0.0.1:5000 or http://localhost:5000 in browser, it has no issue to load the whole page.

However, when I try to open http://0.0.0.0:5000, it first gets the html file successfully. However, in the following requests, it tries to get JS and CSS files through https that do not exist.

I know the <script src="/static/js/main.89f33fbb.chunk.js"> will try to get the file through https first, but if not exist, I expect it to get through http.

I tried Chrome, Firefox, and Safari in private mode, and the results are same.

Is there any place I did wrong?

解决方案

You can listen on any address using 0.0.0.0, but you should not use it to send a request, because 0.0.0.0 is a non-routable meta-address used to designate an invalid, unknown or non-applicable target.

In the context of servers, 0.0.0.0 means "all IPv4 addresses on the local machine". If a host has two ip addresses, 192.168.1.1 and 10.1.2.1, and a server running on the host listens on 0.0.0.0, it will be reachable at both of those IPs, but if you want to reach the server, you should use one of those Ip.

Take a look at:

这篇关于为什么当主机为0.0.0.0时,浏览器为何尝试通过https获取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 10:51