简体   繁体   中英

Struggling to understand how React determines address of API server

I have a project with a client folder containing a React app bootstrapped with create-react-app . I also have a server folder with an express API server running on localhost:80 .

Originally, I ran my frontend and backend as separate servers, making requests to the API server by making requests with fetch("url") and using "proxy": "localhost:80" in my package.json

I have recently altered the project so that my server serves the static frontend files like so: app.use(express.static(path.resolve(__dirname, "../client/build")))

I then tested running the server on two different ports 80 and 3000 , and the frontend and backend both worked perfectly but I believed it would only work when the server was run on port 80

How does my frontend now know where to call the API server when it is running on different ports?

Proxy is used in the development environment and after that when you make build then its convert into the static HTML , CSS and JS files and that can be run in any port using node if you try that in a different port than it will also work as same

Proxy is not made for the production environment

https://github.com/facebook/create-react-app/issues/1087

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM