简体   繁体   中英

React | Package.Json | “proxy” Not Working in Google App Engine

I created a basic React app with 'create-react-app' and a simple Express service. In the react application I added 'proxy' to the Package.json file which points to the Express service url.

 "proxy": <EXPRESS URL HERE>

When testing locally React calls Express through the proxy and everything works. When I deploy to Google App Engine the proxy isn't working. I tested Express service(on App Engine) with my local React instance and things work as expected. When I deploy the React app to App Engine(after running NPM RUN BUILD), the proxy is not working.

Video i watched on getting React/Express working together with 'proxy': https://youtu.be/8bNlffXEcC0

That setting is for development only as per https://create-react-app.dev/docs/proxying-api-requests-in-development

For production you'll need express to serve up both the express API and the React build. You can do this with something like this in your express code:

app.use(express.static(`${__dirname}/../build`)) // serves the react build
app.use('/api', apiRouter) // your api code

Now when you start up your express server it will serve up your API and your build code at once.

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