A minimal example of using a ExpressJs backend (server for API, proxy, & routing) with a React frontend.
To deploy a frontend-only React app, use the static-site optimized
A combo of two npm projects, the backend server and the frontend UI. So there are two package.json configs and thereforce two places to run npm commands:
package.jsonfor Node server & Heroku deployheroku-postbuildscript compiles the webpack bundle during deploycacheDirectoriesincludesreact-ui/node_modules/to optimize build time
react-ui/package.jsonfor React web UI- generated by create-react-app
Includes a minimal Node Cluster implementation to parallelize the single-threaded Node process across the available CPU cores.
Demo deployment: example API call from the React UI is fetched with a relative URL that is served by an Express handler in the Node server.
git clone https://github.com/GeekRishabh/Host-cra.git
cd Host-cra/
heroku create
git push heroku masterThis deployment will automatically:
- detect Node buildpack
- build the app with
npm installfor the Node serverheroku-postbuildfor create-react-app
- launch the web process with
npm start- serves
../react-ui/build/as static files - customize by adding API, proxy, or route handlers/redirectors
- serves
package-lock.json? We resolved a compatibility issue.
👓 More about deploying to Heroku.
In a terminal:
# Initial setup
npm install
# Start the server
npm startThe React app is configured to proxy backend requests to the local Node server. (See "proxy" config)
In a separate terminal from the API server, start the UI:
# Always change directory, first
cd react-ui/
# Initial setup
npm install
# Start the server
npm start