[英]How to serve a React/Javascript single page application
I have a silly question about web servers and how they serve single page javascript apps. 关于Web服务器以及它们如何为单页javascript应用程序提供服务,我有一个愚蠢的问题。 I have a React app that I created with the React CLI by Facebook (essentially some preconfigured build scripts and webpack config).
我有一个React应用程序,我用Facebook的React CLI创建(基本上是一些预先配置的构建脚本和webpack配置)。
If I open the index.html
file, all I get is a blank page. 如果我打开
index.html
文件,我得到的只是一个空白页面。 Only if I serve the app with something like python3 -m http.server
does the app function. 只有当我使用像
python3 -m http.server
这样的应用程序python3 -m http.server
才能运行应用程序。 Why is this the case? 为什么会这样?
Its pretty easy. 它非常简单。 Whenever you include the script file in you index.html, the browser sends a GET requests to fetch the file as is the case in you react file when you include it as say
每当你在index.html中包含脚本文件时,浏览器就会发送一个GET请求以获取文件,就像你在反应文件中的情况一样,当你包含它时
<script src="./src/bundle.js"></script>
These GET requests cannot fetch static file in your system and thus you need to serve them using a server. 这些GET请求无法在系统中获取静态文件,因此您需要使用服务器来提供它们。 This is the reason you need to use servers such as http-server.
这就是您需要使用http-server等服务器的原因。
To see this you can look at the console and you will find this as an error when you directly open an HTML file. 要查看此信息,您可以查看控制台,当您直接打开HTML文件时,您会发现这是一个错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.