0f8d98090a25521c2ae0590598f3f0edbb75928b
create-react-app with a Node API on Heroku
Example app combining a React UI (from create-react-app) with a custom Node/Express server.
If you simply want to deploy a React app, try create-react-app-buildpack.
Design Points
- Node server
package.json, at the rootpostinstallscript performs React app'snpm installon deploy to HerokucacheDirectoriesincludesreact-ui/node_modules/to speed up subsequent deploys to Heroku
- serves
../react-ui/build/as static files npmworks normally
- React app
react-ui/package.json, in a subdirectory- as generated by create-react-app
- plus the
proxyconfig enables dual-process (hot-reloading React UI + Node API) for local development
npmmust be run against the subdirectory- use the prefix option,
npm start --prefix react-ui, or - change directories
cd react-ui/to usenpm
- use the prefix option,
- Heroku
- heroku/node buildpack will auto-detect via
package.json npm installfor server &postinstallfor React UI will automatically runnpm startwill automatically launch
- heroku/node buildpack will auto-detect via
Punchline
Demo deployment: https://cra-node.herokuapp.com/
Example API call is fetched from relative URL.
Deploy to Heroku
git clone https://github.com/mars/heroku-cra-node.git
cd heroku-cra-node/
heroku create
git push heroku master
Local Development
Run the API Server
In a terminal:
# Initial setup
npm install
# Start the server
npm start
Run the React UI
The 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:
# Initial setup
npm install --prefix react-ui
# Start the server
npm start --prefix react-ui
Description
Languages
JavaScript
39.8%
HTML
38.7%
CSS
21.5%