-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (47 loc) · 1.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Created by axetroy on 17-4-26.
*/
const express = require('express');
const axios = require('axios');
const bodyParser = require('body-parser');
const app = express();
const PORT = process.env.PORT || 3000;
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
next();
});
app.get('/', function(req, res) {
res.send('Hello World.');
});
app.get('/oauth', function(req, res) {
const code = req.query.code;
axios
.post(
'https://github.com/login/oauth/access_token',
{
code,
client_id: 'b8257841dd7ca5eef2aa',
client_secret: '4da33dd6fcb0a01d395945ad18613ecf9c12079e'
},
{
responseType: 'json',
headers: {
Accept: 'application/json'
}
}
)
.then(function(response) {
res.status(200).json(response.data);
})
.catch(function(err) {
res.status(400).json({ err: err + '' });
});
});
app.listen(PORT, function() {
console.log(`Listen the port: ${PORT}`);
});