• Stars
    star
    938
  • Rank 48,718 (Top 1.0 %)
  • Language
    HTML
  • License
    MIT License
  • Created almost 8 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

⚛️ How to use create-react-app with a custom Node server on Heroku

create-react-app with a Node server on Heroku

A minimal example of using a Node backend (server for API, proxy, & routing) with a React frontend.

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 server and the frontend UI. So there are two package.json configs and thereforce two places to run npm commands:

  1. Node server: ./package.json
  2. React UI: react-ui/package.json

Includes a minimal Node Cluster implementation to parallelize the single-threaded Node process across the available CPU cores.

Demo

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.

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
    • npm run build for 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

⚠️ Using npm 5’s new package-lock.json? We resolved a compatibility issue. See PR for more details.

👓 More about deploying to Heroku.

Switching from create-react-app-buildpack

If an app was previously deployed with create-react-app-buildpack, then a few steps are required to migrate the app to this architecture:

  1. Remove create-react-app-buildpack from the app; heroku/nodejs buildpack will be automatically activated

    heroku buildpacks:clear
  2. Move the root React app files (including dotfiles) into a react-ui/ subdirectory

    mkdir react-ui
    git mv -k [!react-ui]* react-ui/
    mv node_modules react-ui/
    
    # If you see "fatal: Not a git repository", then fix that error
    mv react-ui/.git ./

    ⚠️ Some folks have reported problems with these commands. Using the bash shell will probably allow them to work. Sorry if they do not work for you, know that the point is to move everything in the repo into the react-ui/ subdirectory. Except for .git/ which should remain at the root level. 

  3. Create a root package.json, server/, & .gitignore modeled after the code in this repo

  4. Commit and deploy ♻️

    git add -A
    git commit -m 'Migrate from create-react-app-buildpack to Node server'
    git push heroku master
  5. If the app uses Runtime configuration, then follow Runtime config below to keep it working.

Runtime Config

create-react-app itself supports configuration with environment variables. These compile-time variables are embedded in the bundle during the build process, and may go stale when the app slug is promoted through a pipeline or otherwise changed without a rebuild. See create-react-app-buildpack's docs for further elaboration of compile-time vs runtime variables.

create-react-app-buildpack's runtime config makes it possible to dynamically change variables, no rebuild required. That runtime config technique may be applied to Node.js based apps such as this one.

  1. Add the inner buildpack to your app, so that the heroku/nodejs buildpack is last:

    heroku buildpacks:add -i 1 https://github.com/mars/create-react-app-inner-buildpack
    
    # Verify that create-react-app-inner-buildpack comes before nodejs
    heroku buildpacks
  2. Set the bundle location for runtime config injection:

    heroku config:set JS_RUNTIME_TARGET_BUNDLE='/app/react-ui/build/static/js/*.js'
  3. Now, build the app with this new setup:

    git commit --allow-empty -m 'Enable runtime config with create-react-app-inner-buildpack'
    git push heroku master

Local Development

Because this app is made of two npm projects, there are two places to run npm commands:

  1. Node API server at the root ./
  2. React UI in react-ui/ directory.

Run the API server

In a terminal:

# Initial setup
npm install

# Start the server
npm start

Install new npm packages for Node

npm install package-name --save

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

Install new npm packages for React UI

# Always change directory, first
cd react-ui/

npm install package-name --save

More Repositories

1

create-react-app-buildpack

⚛️ Heroku Buildpack for create-react-app: static hosting for React.js web apps
Shell
3,323
star
2

heroku-nextjs

⏩ Deploy Next.js universal web apps to Heroku
HTML
360
star
3

heroku-nextjs-custom-server-express

Deploy Next.js server-side React apps using a custom Express server to Heroku
JavaScript
93
star
4

heroku-js-runtime-env

Runtime env var support for static javascript apps on Heroku
JavaScript
28
star
5

create-react-app-inner-buildpack

Inner layer of Heroku Buildpack for create-react-app
Ruby
25
star
6

diesel-pg-timestamp-usage

🔬 Working sample of UTC timestamps with Rust/Diesel/Postgres
Rust
21
star
7

heroku-nextjs-build

Heroku build adapter for Next.js (an npm module)
Shell
12
star
8

episode-7

A facade for side-effects: write test-friendly, async/await-style, JS code
JavaScript
10
star
9

event-driven-functions

🚇 Invoke javascript functions in a Heroku app via Salesforce Platform Events
JavaScript
9
star
10

terraforming-app

☁️🌱 Run Terraform CLI in the cloud: `heroku run terraform apply`
HCL
6
star
11

heroku-buildpack-gnu-parallel

A shell tool for executing jobs in parallel
Shell
5
star
12

persistent-state-machine

🔬 Research toward a database-backed finite state machine in Rust language
Rust
5
star
13

heroku-js-runtime-env-buildpack

🔬🚧 Research towards generalization of "JS Runtime Env" from create-react-app-buildpack
Ruby
3
star
14

make-connected-app-jwt

Helper command to generate & deploy a Salesforce Connected App with JWT auth for Admins
JavaScript
3
star
15

typical_situation

The missing Ruby on Rails ActionController REST API mixin.
Ruby
3
star
16

terraforming-buildpack

Use Terraform in a Heroku app
Shell
2
star
17

tinyrobot-science-web-ui

Next.js web UI for 🤖🔬 tinyrobot.science
JavaScript
2
star
18

WaveHeart

audio player built in MacRuby using OS X AudioQueue
Ruby
2
star
19

terraform-heroku-aws-vpc

AWS VPC ready for 🍐 Heroku Private Spaces
HCL
2
star
20

reqinspeq

Web listener that logs complete HTTP requests for inspection
JavaScript
2
star
21

peer-health-checker

A web app that tests the health of a connection between two networks
JavaScript
2
star
22

wasabi-internal

❇️🕓 Example backend microservice intended for a private network 🤫
JavaScript
1
star
23

tinyrobot-science-api

Node.js API for 🤖🔬 tinyrobot.science
JavaScript
1
star
24

jsforce-patient-bulk-op

Patiently bulk insert/upsert to Salesforce via jsforce; adjustable timeout & poll frequency
JavaScript
1
star
25

cra-example-app

The default app generated by create-react-app
JavaScript
1
star
26

seesaw

simple reverse network proxy (load balancer) shell tool; built with Ruby + EventMachine
Ruby
1
star
27

crak-buildpack

⚛️🐒 Heroku Buildpack for create-react-app: host React.js web apps with a Kong API gateway
Shell
1
star
28

terraform-pg-state-store

Terraform pg state store using Heroku Postgres
HCL
1
star
29

ferry-schedule

Experiments w/ React (JS & Native)
JavaScript
1
star
30

tinyrobot-science-mesh-web-ui

🚧🔬 experiment using Kong service mesh in a Heroku app
JavaScript
1
star
31

couchkicker

A CouchDB "update_notification" client to keep views fresh.
JavaScript
1
star