Skip to content

Commit 0008038

Browse files
committed
Updates to allow for deployment to Heroku
* Separate production and deploy boot processes * Add `client/build` to source control * Unify ports around environment variable `PORT`
1 parent 9a5dba9 commit 0008038

20 files changed

+678
-13
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
cra-server-ex.graffle
22
node_modules
33
npm-debug.log
4-
client/build

Procfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
web: cd client && npm start
2-
api: npm run server
1+
web: npm run server

Procfile.dev

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
web: cd client && npm start
2+
api: PORT=3001 npm run server

README.md

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ We have a [detailed blog post](https://www.fullstackreact.com/articles/using-cre
1212

1313
Check out the [Rails version](https://github.com/fullstackreact/food-lookup-demo-rails) if that's your preferred API server platform.
1414

15-
## Running
15+
## Running locally
1616

1717
```
1818
git clone [email protected]:fullstackreact/food-lookup-demo.git
@@ -23,9 +23,11 @@ cd client
2323
npm i
2424
2525
cd ..
26-
npm start
26+
npm run dev
2727
```
2828

29+
**Important**: **`npm start`** is intended for production only. Use `npm run dev`.
30+
2931
## Overview
3032

3133
`create-react-app` configures a Webpack development server to run on `localhost:3000`. This development server will bundle all static assets located under `client/src/`. All requests to `localhost:3000` will serve `client/index.html` which will include Webpack's `bundle.js`.
@@ -41,7 +43,7 @@ return fetch(`/api/food?q=${query}`, {
4143
})
4244
```
4345

44-
This request is made to `localhost:3000`, the Webpack dev server. Because the route has the special prefix `/api/`, the Webpack server knows that this request is actually intended for our API server. We specify in `package.json` that we would like Webpack to proxy API requests to `localhost:3001`:
46+
This request is made to `localhost:3000`, the Webpack dev server. Webpack will infer that this request is actually intended for our API server. We specify in `package.json` that we would like Webpack to proxy API requests to `localhost:3001`:
4547

4648
```js
4749
// Inside client/package.json
@@ -57,11 +59,83 @@ Therefore, the user's browser makes a request to Webpack at `localhost:3000` whi
5759
This setup provides two advantages:
5860

5961
1. If the user's browser tried to request `localhost:3001` directly, we'd run into issues with CORS.
60-
2. In many setups, this means that references to the API URL in development matches that in production. You don't have to do something like this:
62+
2. The API URL in development matches that in production. You don't have to do something like this:
6163

6264
```js
6365
// Example API base URL determination in Client.js
6466
const apiBaseUrl = process.env.NODE_ENV === 'development' ? 'localhost:3001' : '/'
6567
```
6668

6769
This setup uses [node-foreman](https://github.com/strongloop/node-foreman) for process management. Executing `npm start` instructs Foreman to boot both the Webpack dev server and the API server.
70+
71+
## Deploying
72+
73+
### Background
74+
75+
The app is ready to be deployed to Heroku. In `package.json`, we specify separate boot commands for development and production:
76+
77+
```
78+
"start": "nf start -p $PORT",
79+
"dev": "nf start -p 3000 --procfile Procfile.dev",
80+
```
81+
82+
In development, we use `Procfile.dev` which boots both the API server and the Webpack server:
83+
84+
```
85+
web: cd client && npm start
86+
api: PORT=3001 npm run server
87+
```
88+
89+
In production, Heroku will use `Procfile` which boots just the server:
90+
91+
```
92+
web: npm run server
93+
```
94+
95+
Inside `server.js`, we tell Node/Express we'd like it to serve static assets in production:
96+
97+
```
98+
if (process.env.NODE_ENV === 'production') {
99+
app.use(express.static('client/build'));
100+
}
101+
```
102+
103+
### Steps
104+
105+
We assume basic knowledge of Heroku.
106+
107+
**0. Setup your Heroku account and Heroku CLI**
108+
109+
For installing the CLI tool, see [this article](https://devcenter.heroku.com/articles/heroku-command-line).
110+
111+
**1. Build the React app**
112+
113+
Running `npm run build` creates the static bundle which we can then use any HTTP server to serve:
114+
115+
```
116+
cd client/
117+
npm run build
118+
```
119+
120+
**2. Commit the `client/build` folder to source control**
121+
122+
From the root of the project:
123+
124+
```
125+
git add client/build
126+
git commit -m 'Adding `build` to source control'
127+
```
128+
129+
**3. Create the Heroku app**
130+
131+
```
132+
heroku apps:create food-lookup-demo
133+
```
134+
135+
**4. Push to Heroku**
136+
137+
```
138+
git push heroku master
139+
```
140+
141+
Heroku will give you a link at which to view your live app.

client/.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ node_modules
66
# testing
77
coverage
88

9-
# production
10-
build
11-
129
# misc
1310
.DS_Store
1411
.env

client/build/asset-manifest.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"main.css": "static/css/main.03b6e96f.css",
3+
"main.css.map": "static/css/main.03b6e96f.css.map",
4+
"main.js": "static/js/main.e5a5c899.js",
5+
"main.js.map": "static/js/main.e5a5c899.js.map",
6+
"static/media/flags.png": "static/media/flags.9c74e172.png",
7+
"static/media/icons.eot": "static/media/icons.f7c2b4b7.eot",
8+
"static/media/icons.svg": "static/media/icons.29800836.svg",
9+
"static/media/icons.ttf": "static/media/icons.706450d7.ttf",
10+
"static/media/icons.woff": "static/media/icons.d9ee23d5.woff",
11+
"static/media/icons.woff2": "static/media/icons.97493d3f.woff2"
12+
}

client/build/favicon.ico

24.3 KB
Binary file not shown.

client/build/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="shortcut icon" href="/favicon.ico"><title>Food Lookup Demo</title><link href="/static/css/main.03b6e96f.css" rel="stylesheet"></head><body><div id="root"></div><script type="text/javascript" src="/static/js/main.e5a5c899.js"></script></body></html>

client/build/static/css/main.03b6e96f.css

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/build/static/css/main.03b6e96f.css.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/build/static/js/main.e5a5c899.js

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/build/static/js/main.e5a5c899.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
27.5 KB
Loading

0 commit comments

Comments
 (0)