2017-02-06 20:01:58 -08:00
🌱
2017-01-30 14:46:34 -08:00
🌱
2017-01-30 14:46:34 -08:00
🌱
2017-01-30 14:46:34 -08:00
🌱
2017-01-30 14:46:34 -08:00
2017-02-06 20:01:58 -08:00

create-react-app with a Node server on Heroku

A minimal example of using a Node backend (API, proxy, & routing) with a React app and an Express server.

To deploy a frontend-only React app, use the static-site optimized ▶️ create-react-app-buildpack

Design Points

A combo of two npm projects, the backend and the frontend. So there are two package.json configs.

  1. package.json for Node server & Heroku deploy
    • postinstall script builds the React UI webpack bundle
    • cacheDirectories includes react-ui/node_modules/ to optimize deploy to Heroku
  2. react-ui/package.json for React web UI

Punchline

Demo deployment.

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

This deployment will automatically:

  • detect Node buildpack
  • build the app with
    • npm install for the Node server
    • postinstall to build the webpack bundle from 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

👓 More about deploying to Heroku.

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:

# Always change directory, first
cd react-ui/

# Initial setup
npm install

# Start the server
npm start
Description
No description provided
Readme 501 KiB
Languages
JavaScript 39.8%
HTML 38.7%
CSS 21.5%