Skip to content

Commit 6c0dd2b

Browse files
committed
Add Proxy To An API Server In Development With CRA as a react til
1 parent b261ba3 commit 6c0dd2b

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
1010
For a steady stream of TILs from a variety of rocketeers, checkout
1111
[til.hashrocket.com](https://til.hashrocket.com/).
1212

13-
_578 TILs and counting..._
13+
_579 TILs and counting..._
1414

1515
---
1616

@@ -411,6 +411,7 @@ _578 TILs and counting..._
411411
- [Force A Component To Only Have One Child](react/force-a-component-to-only-have-one-child.md)
412412
- [Inline Style Attributes Should Be Camel Cased](react/inline-style-attributes-should-be-camel-cased.md)
413413
- [Passing Props Down To React-Router Route](react/passing-props-down-to-react-router-route.md)
414+
- [Proxy To An API Server In Development With CRA](react/proxy-to-an-api-server-in-development-with-cra.md)
414415

415416
### Ruby
416417

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Proxy To An API Server In Development With CRA
2+
3+
[create-react-app](https://github.com/facebookincubator/create-react-app) is
4+
a great way to bootstrap a React project, especially if you are building a
5+
single-page app. When building an SPA, you more likely than not will have a
6+
backend API that you interact with.
7+
8+
You can set up your React app to interact with that backend API server in
9+
development using the `proxy` configuration in `package.json`.
10+
11+
```json
12+
// package.json
13+
...
14+
"proxy": "http://localhost:4000",
15+
}
16+
```
17+
18+
This will allow you to keep your API calls nice and clean.
19+
20+
```javascript
21+
fetch("/api/session", ...
22+
```
23+
24+
No need to manage some sort of _host URL_ environment variable.
25+
26+
Additionally, this will remove an CORS issues because the `webpackDevServer`
27+
will be proxying any paths that it doesn't recognize to the host and port
28+
that you've specified.
29+
30+
See [the `create-react-app`
31+
docs](https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#proxying-api-requests-in-development)
32+
for more details.

0 commit comments

Comments
 (0)