File tree 2 files changed +34
-1
lines changed
2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
10
10
For a steady stream of TILs from a variety of rocketeers, checkout
11
11
[ til.hashrocket.com] ( https://til.hashrocket.com/ ) .
12
12
13
- _ 578 TILs and counting..._
13
+ _ 579 TILs and counting..._
14
14
15
15
---
16
16
@@ -411,6 +411,7 @@ _578 TILs and counting..._
411
411
- [ Force A Component To Only Have One Child] ( react/force-a-component-to-only-have-one-child.md )
412
412
- [ Inline Style Attributes Should Be Camel Cased] ( react/inline-style-attributes-should-be-camel-cased.md )
413
413
- [ 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 )
414
415
415
416
### Ruby
416
417
Original file line number Diff line number Diff line change
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.
You can’t perform that action at this time.
0 commit comments