A minimal example of using a Node backend Blogging App (server for API, proxy, & routing) with a React frontend.
To deploy a frontend-only React app, use the static-site optimized
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:
- Node server:
./package.json
- deployed automatically via heroku/nodejs buildpack
- React UI:
build/package.json
- generated by create-react-app
- deployed via
build
script in the Node server's./package.json
- module cache configured by
cacheDirectories
Includes a minimal Node Cluster implementation to parallelize the single-threaded Node process across the available CPU cores.
- Server code is implemented with express library.
- The server is RESTful.
- The database used is MongoDB which is a so-called document database.
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.
heroku create
git push heroku master
This deployment will automatically:
- detect Node buildpack
- build the app with
npm install
for the Node servernpm run build
for create-react-app
- launch the web process with
npm start
- serves
../build/build/
as static files - customize by adding API, proxy, or route handlers/redirectors
- serves
package-lock.json
? We resolved a compatibility issue. See PR for more details.
👓 More about deploying to Heroku.
Because this app is made of two npm projects, there are two places to run npm
commands:
- Node API server at the root
./
- React UI in
build/
directory.
In a terminal:
# Initial setup
npm install
# Start the server
npm start
npm install package-name --save
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 build/
# Initial setup
npm install
# Start the server
npm start
# Always change directory, first
cd build/
npm install package-name --save
In a terminal:
# Initial setup
npm install
# Start the server
npm start
npm install package-name --save