Skip to content

Commit 731908d

Browse files
authored
Document the proxy option
1 parent 37fcce7 commit 731908d

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

template/README.md

+48-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ You can find the most recent version of this guide [here](https://github.com/fac
2020
- [Adding Flow](#adding-flow)
2121
- [Adding Custom Environment Variables](#adding-custom-environment-variables)
2222
- [Integrating with a Node Backend](#integrating-with-a-node-backend)
23+
- [Proxying API Requests in Development](#proxying-api-requests-in-development)
2324
- [Deployment](#deployment)
2425
- [Now](#now)
2526
- [Heroku](#heroku)
@@ -462,8 +463,54 @@ if (process.env.NODE_ENV !== 'production') {
462463
463464
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).
464465
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.html with React app
475+
/todos - static server returns index.html with 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+
465503
## Deployment
466504
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+
467514
### Now
468515
469516
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
476523
477524
>Note: this feature is available with `react-scripts@0.2.0` and higher.
478525
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:
481527
482528
```js
483-
{
484-
"name": "my-app",
485529
"homepage": "http://myusername.github.io/my-app",
486-
// ...
487-
}
488530
```
489531
490532
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

Comments
 (0)