Skip to content

Commit 6f08c76

Browse files
committed
Merge pull request #751 from chadoh/patch-1
Create ApiConfig.md explaining use of a real API
2 parents 0fe749f + 6471b00 commit 6f08c76

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

docs/ApiConfig.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Switching to a real API
2+
3+
Chances are, once you get comfortable with this setup, you'll want to hook into some existing API rather than use the simple one that comes with this project. Here's how:
4+
5+
## Update `package.json`
6+
7+
First things first, you need to add `APIHOST` settings in `package.json`. If you look in `src/config.js`, you'll see that it's already configured to read this `APIHOST` setting if it's present.
8+
9+
If the port you use differs between your dev & prod API hosts, you may want to get rid of the `APIPORT` setting, including it right in `APIHOST`. Same with the protocol – if you use HTTP in dev but HTTPS in prod, you may want to include the protocol right in `APIHOST`, and then get rid of the explicit `"http://"` found in the next section.
10+
11+
## Update `ApiClient`
12+
13+
Open up `src/helpers/ApiClient.js`. You'll see this line:
14+
15+
``` javascript
16+
if (__SERVER__) {
17+
// Prepend host and port of the API server to the path.
18+
return 'http://' + config.apiHost + adjustedPath;
19+
}
20+
```
21+
22+
If you added `http://` or `https://` to your APIHOST setting, then you need to remove it here.
23+
24+
In this file, you'll also see that there's a `/api` that gets prepended to the URL when on the client side. That gets routed through a proxy that's configured in server.js, which we'll get to next.
25+
26+
Why do you need a proxy? So that the `APIHOST` can be set as part of the Node environment, and your client side code can still work. A user's browser doesn't have access to your server's Node environment, so instead the client-side code makes all API calls to this `/api` proxy, which the server configures to hit your real API. That way you can control everything sanely, through environment variables, and set different API endpoints for your different environments.
27+
28+
## Update `server.js`
29+
30+
To update the proxy, find this chunk in `src/server.js`:
31+
32+
``` javascript
33+
const proxy = httpProxy.createProxyServer({
34+
target: 'http://' + config.apiHost,
35+
ws: true
36+
});
37+
```
38+
39+
You'll want to change this in a few ways:
40+
41+
### 1. Update `target` protocol
42+
43+
If you changed APIHOST to include the `http://` or `https://` protocol, then get rid of the `'http://' +` in the `target` setting. Note that you'll need to restart your server after making these changes or things will break.
44+
45+
### 2. Decide if you need WebSockets
46+
47+
The `ws: true` setting is there to support WebSockets connections, which this demo app supports using [socket.io](http://socket.io/). If your API doesn't use WebSockets, then you can remove this line.
48+
49+
### 3. Add a `changeOrigin` setting
50+
51+
This might be the most important part! It's possible that your API lives at a totally different URL than your front-end app. If that's the case, then you need to add a `changeOrigin` setting.
52+
53+
Here's an example of what the final chunk of code might look like:
54+
55+
``` javascript
56+
const proxy = httpProxy.createProxyServer({
57+
target: config.apiHost,
58+
changeOrigin: true
59+
});
60+
```
61+
62+
Finally, after doing all of that, you can get rid of the demo API.
63+
64+
## Get rid of stuff
65+
66+
You can remove the whole `api` folder, as well as `bin/api.js`.
67+
68+
Once you do that, you'll also need to remove the lines in `package.json` that called those things. Remove all this:
69+
70+
* ` \"npm run start-prod-api\"` from the `start` script command
71+
* ` \"npm run start-dev-api\"` from the `dev` script command
72+
* the `start-prod-api` and `start-dev-api` scripts altogether
73+
* the ` api` argument from the `lint` script
74+
* the `test-node` and `test-node-watch` scripts, which were there to test the demo API
75+
* the `start-prod-api` and `start-dev-api` settings in the `betterScripts` section
76+
77+
If you want, you can also remove all references to `socket`, if you're not using it.

0 commit comments

Comments
 (0)