简体   繁体   English

如何提供React / Javascript单页面应用程序

[英]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.

相关问题 如何使用 JavaScript 刷新单页应用程序? - How to refresh single page application with JavaScript? 如何正确地导入和使用MSAL(用于js的Microsoft身份验证库)到javascript(非打字稿)反应单页应用程序中? - How to properly import and use the MSAL (Microsoft Authentication Library for js) into a javascript (non typescript) react single page application? Javascript React 单页应用程序 + Amazon S3:永久链接问题 - Javascript React Single Page Application + Amazon S3: Permalinks issue 单页面应用程序的体系结构(JavaScript) - Architecture for single page application (JavaScript) 单页应用程序的phonegap中的javascript - javascript in phonegap for single page application 在单页应用程序中调试JavaScript - Debugging javascript in a single page application 如何在反应单页应用程序中隐藏 XHR 请求? - How to hide XHR Requests in a react single page application? 如何部署React JS单页应用程序(SPA) - How to deploy react js single page application (SPA) 如何使用react-router创建单页应用程序 - How to create a Single Page Application using react-router 如何在JavaScript中设计单页应用程序以在环境模式下工作? - How to design single page application in javascript to work in environment mode?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM