Ben
Ben

Reputation: 759

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. I have a React app that I created with the React CLI by Facebook (essentially some preconfigured build scripts and webpack config).

If I open the index.html file, all I get is a blank page. Only if I serve the app with something like python3 -m http.server does the app function. Why is this the case?

Upvotes: 1

Views: 120

Answers (1)

Shubham Khatri
Shubham Khatri

Reputation: 281626

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

<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. This is the reason you need to use servers such as 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.

Upvotes: 1

Related Questions