Giter VIP home page Giter VIP logo

heroku-cra-node's Introduction

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

⤵️ Switching from 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.

  1. package.json for Node server & Heroku deploy
    • heroku-postbuild script compiles the webpack bundle during deploy
    • cacheDirectories includes react-ui/node_modules/ to optimize build time
  2. react-ui/package.json for React web UI

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
    • heroku-postbuild 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 [!react-ui]* react-ui/
    # You'll see "fatal: Not a git repository"; let's fix that error
    mv react-ui/.git ./
  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

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

heroku-cra-node's People

Contributors

mars avatar timer avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.