Skip to content

Commit 2a4fe33

Browse files
committed
Fetch API Data While Keeping Keys Secure
1 parent 0a624c0 commit 2a4fe33

File tree

6 files changed

+52
-9
lines changed

6 files changed

+52
-9
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
npm-debug.log*
1616
yarn-debug.log*
1717
yarn-error.log*
18-
18+
keys.json

index.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
11
import compression from 'compression';
22
import express from 'express';
3+
import fs from 'fs';
4+
import request from 'request';
35

46
const app = express();
57
const port = process.env.PORT || 8080;
68
app.use(compression());
79

8-
app.use(express.static('./build'));
10+
let keys;
11+
if (fs.existsSync('./keys.json')) {
12+
keys = require('./keys.json');
13+
} else {
14+
keys = JSON.parse(process.env.VCAP_SERVICES)['user-provided'][0].credentials;
15+
}
16+
17+
app.get('/github', (req, res) => {
18+
request({
19+
url: `https://api.github.com/user/repos?affiliation=owner,collaborator&access_token=${keys.github}`,
20+
headers: {
21+
'user-agent': 'node.js',
22+
},
23+
}, (err, response, body) => {
24+
if (!err && response.statusCode === 200) {
25+
res.send(body);
26+
}
27+
});
28+
});
929

30+
app.use(express.static('./build'));
1031
app.listen(port, (err) => {
11-
if (err) {
12-
console.log(err);
13-
return;
14-
}
15-
console.log(`Server is live at http://localhost:${port}`);
32+
if (err) {
33+
console.log(err);
34+
return;
35+
}
36+
console.log(`Server is live at http://localhost:${port}`);
1637
});

manifest.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ applications:
55
command: npm run bluemix
66
disk_quota: 256MB
77
memory: 128MB
8+
services:
9+
- keys

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"compression": "^1.6.2",
1010
"express": "^4.15.2",
1111
"react": "^15.4.2",
12-
"react-dom": "^15.4.2"
12+
"react-dom": "^15.4.2",
13+
"request": "^2.80.0"
1314
},
1415
"devDependencies": {
1516
"babel-watch": "^2.0.6",

src/App.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@ import logo from './logo.svg';
33
import './App.css';
44

55
class App extends Component {
6+
7+
state = {
8+
repos: [],
9+
}
10+
11+
componentDidMount() {
12+
fetch('/github')
13+
.then(response => response.json())
14+
.then((data) => {
15+
const repos = data.map((repo) =>
16+
<p key={repo.id}>{repo.name}</p>
17+
);
18+
19+
this.setState({ repos })
20+
});
21+
}
22+
623
render() {
724
return (
825
<div className="App">
@@ -13,6 +30,8 @@ class App extends Component {
1330
<p className="App-intro">
1431
To get started, edit <code>src/App.js</code> and save to reload.
1532
</p>
33+
<h3>App Creator's Repos:</h3>
34+
{this.state.repos}
1635
</div>
1736
);
1837
}

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4638,7 +4638,7 @@ repeating@^2.0.0:
46384638
dependencies:
46394639
is-finite "^1.0.0"
46404640

4641-
request@^2.79.0:
4641+
request, request@^2.79.0:
46424642
version "2.80.0"
46434643
resolved "https://registry.yarnpkg.com/request/-/request-2.80.0.tgz#8cc162d76d79381cdefdd3505d76b80b60589bd0"
46444644
dependencies:

0 commit comments

Comments
 (0)