You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[Integrating with a Node Backend](#integrating-with-a-node-backend)
23
+
-[Proxying API Requests in Development](#proxying-api-requests-in-development)
23
24
-[Deployment](#deployment)
24
25
-[Now](#now)
25
26
-[Heroku](#heroku)
@@ -462,8 +463,54 @@ if (process.env.NODE_ENV !== 'production') {
462
463
463
464
Check out [this tutorial](https://www.fullstackreact.com/articles/using-create-react-app-with-a-server/) for instructions on integrating an app with a Node backend running on another port, and using `fetch()` to access it. You can find the companion GitHub repository [here](https://github.com/fullstackreact/food-lookup-demo).
464
465
466
+
## Proxying API Requests in Development
467
+
468
+
>Note: this feature is available with `react-scripts@0.3.0` and higher.
469
+
470
+
People often serve the front-end React app from the same host and port as their backend implementation.
471
+
For example, a production setup might look like this after the app is deployed:
472
+
473
+
```
474
+
/-static server returns index.htmlwith React app
475
+
/todos -static server returns index.htmlwith React app
476
+
/api/todos - server handles any /api/* requests using the backend implementation
477
+
```
478
+
479
+
Such setup is **not** required. However, if you **do** have a setup like this, it is convenient to write requests like `fetch('/api/todos')` without worrying about redirecting them to another host or port during development.
480
+
481
+
To tell the development server to proxy any unknown requests to your API server in development, add a `proxy` field to your `package.json`, for example:
482
+
483
+
```js
484
+
"proxy": "http://localhost:4000",
485
+
```
486
+
487
+
This way, when you `fetch('/api/todos')` in development, the development server will recognize that it’s not a static asset, and will proxy your request to `http://localhost:4000/api/todos` as a fallback.
488
+
489
+
Conveniently, this avoids [CORS issues](http://stackoverflow.com/questions/21854516/understanding-ajax-cors-and-security-considerations) and error messages like this in development:
490
+
491
+
```
492
+
Fetch API cannot load http://localhost:400/api/todos. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
493
+
```
494
+
495
+
Keep in mind that `proxy` only has effect in development (with `npm start`), and it is up to you to ensure that URLs like `/api/todos` point to the right thing in production. You don’t have to use the `/api` prefix. Any unrecognized request will be redirected to the specified `proxy`.
496
+
497
+
Currently the `proxy` option only handles HTTP requests, and it won’t proxy WebSocket connections.
498
+
If the `proxy` option is **not** flexible enough for you, alternatively you can:
499
+
500
+
* Enable CORS on your server ([here’s how to do it for Express](http://enable-cors.org/server_expressjs.html)).
501
+
* Use [environment variables](#adding-custom-environment-variables) to inject the right server host and port into your app.
502
+
465
503
## Deployment
466
504
505
+
By default, Create React App produces a build assuming your app is hosted at the server root.
506
+
To override this, specify the `homepage` in your `package.json`, for example:
507
+
508
+
```js
509
+
"homepage": "http://mywebsite.com/relativepath",
510
+
```
511
+
512
+
This will let Create React App correctly infer the root path to use in the generated HTML file.
513
+
467
514
### Now
468
515
469
516
See [this example](https://github.com/xkawi/create-react-app-now) for a zero-configuration single-command deployment with [now](https://zeit.co/now).
@@ -476,15 +523,10 @@ Use the [Heroku Buildpack for create-react-app](https://github.com/mars/create-r
476
523
477
524
>Note: this feature is available with `react-scripts@0.2.0` and higher.
478
525
479
-
First, open your `package.json` and add a `homepage` field.
480
-
It could look like this:
526
+
Open your `package.json` and add a `homepage` field:
481
527
482
528
```js
483
-
{
484
-
"name":"my-app",
485
529
"homepage": "http://myusername.github.io/my-app",
486
-
// ...
487
-
}
488
530
```
489
531
490
532
Now, whenever you run `npm run build`, you will see a cheat sheet with a sequence of commands to deploy to GitHub pages:
0 commit comments