-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
79 lines (67 loc) · 1.87 KB
/
server.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var http = require('http');
const axios = require('axios');
var express = require('express');
var app = express();
var fs = require('fs');
const port = 8080;
var baseURL = 'https://na1.api.riotgames.com/lol/';
var apiKey = 'RGAPI-94236ef3-39fa-4112-80d7-ea1a480207c9';
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8000');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Pass to next layer of middleware
next();
});
app.get('/summoner', function (req, res) {
var name = req.param("name");
getSummonerInfo(req, res, name);
});
app.get('/matchlist', function (req, res) {
console.log(req);
var id = req.param("accountId");
getMatchlist(req, res, id);
});
app.listen(port, function () {
console.log(`Server running at http://localhost:${port}/`)
});
function getSummonerInfo(req, res, name) {
var url = `${baseURL}summoner/v3/summoners/by-name/${name}?api_key=${apiKey}`;
axios.get(url)
.then(response => {
res.send(response.data);
console.log(response);
})
.catch(error => {
res.send(error);
console.log(error);
});
}
function getMatchlist(req, res, id) {
console.log("1!!!");
var url = `${baseURL}match/v3/matchlists/by-account/${id}?api_key=${apiKey}`;
console.log(url);
console.log("2!!!");
axios.get(url)
.then(response => {
console.log("3!!!");
fs.writeFile("matchlist_" + id, JSON.stringify(response.data), function(err) {
if(err) {
console.log("4!!!!!!!");
console.log(err);
}
else {
console.log("File saved");
}
});
console.log("5!!!!");
res.send(response.data);
console.log(response);
})
.catch(error => {
res.send(error);
console.log(error);
});
}