All Projects → mars → Heroku Cra Node

mars / Heroku Cra Node

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

Projects that are alternatives of or similar to Heroku Cra Node

Create React App Buildpack
⚛️ Heroku Buildpack for create-react-app: static hosting for React.js web apps
Stars: ✭ 3,161 (+284.55%)
Mutual labels:  heroku, create-react-app
Generator Django Rest
Yeoman generator for a Django REST/GraphQL API, an optional React SPA & lots more!
Stars: ✭ 77 (-90.63%)
Mutual labels:  heroku, create-react-app
React App
Create React App with server-side code support
Stars: ✭ 614 (-25.3%)
Mutual labels:  create-react-app
Zws
Shorten URLs using invisible spaces.
Stars: ✭ 780 (-5.11%)
Mutual labels:  heroku
Packtpub Crawler
Download your daily free Packt Publishing eBook https://www.packtpub.com/packt/offers/free-learning
Stars: ✭ 717 (-12.77%)
Mutual labels:  heroku
Python Getting Started
Getting Started with Python on Heroku.
Stars: ✭ 637 (-22.51%)
Mutual labels:  heroku
Node Typescript Koa Rest
REST API boilerplate using NodeJS and KOA2, typescript. Logging and JWT as middlewares. TypeORM with class-validator, SQL CRUD. Docker included. Swagger docs, actions CI and valuable README
Stars: ✭ 739 (-10.1%)
Mutual labels:  heroku
Create Eth App
Create Ethereum-powered apps with one command
Stars: ✭ 597 (-27.37%)
Mutual labels:  create-react-app
Satis On Heroku
Your private Satis instance on Heroku, just one click away.
Stars: ✭ 5 (-99.39%)
Mutual labels:  heroku
React Native Web Monorepo
Code sharing between iOS, Android & Web using monorepo
Stars: ✭ 697 (-15.21%)
Mutual labels:  create-react-app
Jekyll Auth
A simple way to use GitHub OAuth to serve a protected Jekyll site to your GitHub organization
Stars: ✭ 778 (-5.35%)
Mutual labels:  heroku
Cli
Heroku CLI
Stars: ✭ 685 (-16.67%)
Mutual labels:  heroku
Reason Scripts
🔰 Create a ReasonML and React development environment
Stars: ✭ 644 (-21.65%)
Mutual labels:  create-react-app
Reactprimer
React component prototyping tool that generates fully connected class component code.
Stars: ✭ 743 (-9.61%)
Mutual labels:  create-react-app
Express Babel
Express starter kit with ES2017+ support, testing, linting, and code coverage
Stars: ✭ 621 (-24.45%)
Mutual labels:  heroku
Www.ruby Lang.org
Source of the https://www.ruby-lang.org website.
Stars: ✭ 790 (-3.89%)
Mutual labels:  heroku
Heroku Repo
Plugin for heroku CLI that can manipulate the repo
Stars: ✭ 610 (-25.79%)
Mutual labels:  heroku
React Workspaces Playground
⚛️ 🐈 Zero Config Create-React-App Monorepos with Yarn Workspaces, Lerna and React Storybook.
Stars: ✭ 658 (-19.95%)
Mutual labels:  create-react-app
Ghost On Heroku
One-button Heroku deploy for the Ghost blogging platform.
Stars: ✭ 731 (-11.07%)
Mutual labels:  heroku
Error Overlay Webpack Plugin
Catch errors with style 💥✨
Stars: ✭ 821 (-0.12%)
Mutual labels:  create-react-app

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].