Using Create-React-App with Express

Lou on September 12, 2017

Whilst setting up a test app myself, I couldn't find a simple way to deploy Create React App with Express on the same server. It took some tweaki... [Read Full]
 

What exactly does this mean?
"When you want to deploy, just run the production build and serve your app from localhost:8080 and everything will work as planned." ?
The Production build runs on PORT 5000 should we change that to run on port 8080?

 

Hey, ayanez17.

There are two modes your app can run in. When built (port 8080) via npm run build (which creates an index.html) or when running live reload npm run start which runs on port 3000.

When running npm run start your app will proxy from port 3000 to port 8080 automatically. However when you create a production build your entire app should be running from port 8080, as express and the API's are running from the same server (in this setup).

You shouldn't need to change any ports. Does that help?

 

Quick question! If you don't want to develop and test the React app and the API at the same time, is the proxy step necessary?

Also, how would API requests look like on the React app? With or without proxy.

 

Hey Sammy!

All of the requests are for absolute paths. So for the ping example would just be:

/ping

Then node works out to send it to the right place. This maps the create react app on port 3000 to the api requests on 8080.

If you want them on a separate server you'll need to call with the port number, or domain that you setup your api server on.

Like so:

yourdomain:8080/ping

With that option, however you'll neeed to fiddle with your API access control. So that you only allow access to your app, not the whole internet. However, this can be more tricky and fiddly and depends a lot on how you setup your architecture.

 

hey Lou, thanks for the article.
I'm trying this to see if I can get some environment variables to my static app, but I can't seem to get it to work. I'm assuming I have to pass them in to the client some how.
Do you have any ideas?

thx

 

Hi @Lou, when i deploy to server, it had an error: Error: ENOENT: no such file or directory, stat '/home/namld/projects/raca/build/index.html'. How to fix it?

 

make sure you run npm run build before, otherwise you will not have a build directory

 

Is it possible to serve the "public/index.html" instead of "build" for development, edit my react files, and still have the site on localhost:8080 catch the updates?

code of conduct - report abuse