-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
76 lines (54 loc) · 1.8 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
// Module imports
import express from 'express';
import { fileURLToPath } from 'url';
import path, { dirname } from 'path';
import { loadScorecardData } from "./data_utils.js";
import fs from "fs";
// define __filename and __dirname for use by template renderer
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Create and configure express app
const app = express();
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, '/views'));
/**
* Add logging for all incoming requests
*/
app.use((req, res, next) => {
console.log(`Received ${req.method} request to ${req.url}`);
next();
});
/**
* Request handler for all static file requests
*/
app.use('/static', express.static("static"));
/**
* Request handler for the main webpage. Responds to GET
* requests to the / endpoint. Serves index.html
*/
app.get("/", function (request, response) {
// Render svg chart into template and return
response.render("index");
})
/**
* Request handler to get scorecard data for a tournament & year. Returns
* condensed scorecard data for each player in a JSON object
*/
app.get("/scorecards/year/:year/event/:event", function (request, response) {
// Load the data for the requested event and return as JSON
let data = loadScorecardData(request.params.year, request.params.event);
response.json(data);
})
/**
* Request handler to get event ids and names for each year that scorecard
* data is available
*/
app.get("/events/manifest", function (request, response) {
// Load event manifest and return
const scorecardsRaw = fs.readFileSync("data/event_manifest.json", 'utf8');
response.json(JSON.parse(scorecardsRaw));
})
// Start the server
app.listen(3000, function () {
console.log("App available at http://localhost:3000")
})