-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
57 lines (45 loc) · 1.92 KB
/
app.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
//app.js
const express = require('express'); // web framework
const fetch = require('node-fetch'); // for making AJAX requests
const path = require('path');
// put environmental variables defined in .env file on process.env
require('dotenv').config();
const app = express();
// serve files / assets from the dist folder
app.use(express.static('dist'));
// // in response to `GET /` requests, send the file `dist/index.html`
app.get('/', (request, response) => {
response.sendFile(`${__dirname}/dist/index.html`);
});
// Heroku sets process.env.PORT in production; use 8000 in dev
const PORT = process.env.PORT || 8000;
// start up a server listening at PORT; on success, log a message
app.listen(PORT, () => {
console.log(`Listening at localhost:${PORT}`);
});
app.get('/search', (request, response) => {
const urlStart = 'https://balldontlie.io/api/v1/players?';
const searchTerm = request.query.searchTerm; // from query string
const url = `${urlStart}search=${searchTerm}`;
console.log(`Fetching: ${url}`);
fetch(url) // AJAX request to API
.then(apiResponse => apiResponse.json())
.then(data => response.send(data))
.catch(error => response.send(error));
});
app.get('/season-average', (request, response) => {
const urlStart = 'https://balldontlie.io/api/v1/season_averages?';
const season = request.query.season; // from query string
const playerIds = JSON.parse(request.query.playerId); //from query string
//Create playerIdString for url from playerIds passed in from front end
let playerIdString = "";
playerIds.forEach((id) => {
playerIdString += `&player_ids[]=${id}`;
})
const url = `${urlStart}season=${season}${playerIdString}`;
console.log(`Fetching: ${url}`);
fetch(url) // AJAX request to API
.then(apiResponse => apiResponse.json())
.then(data => response.send(data))
.catch(error => response.send(error));
});