-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
63 lines (53 loc) · 1.47 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
55
56
57
58
59
60
61
62
63
const express = require("express");
const asyncHandler = require("express-async-handler");
const bodyParser = require("body-parser");
const api = require("./utils/api");
const app = express();
const PORT = process.env.PORT || 3000;
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get("/", (req, res) => res.send("Hello World!"));
app.get("/schedules", (req, res, next) => {
const schedules = api.getAllSchedule(req.query);
res.send({
status: "ok",
data: schedules
});
});
app.get("/countries", (req, res, next) => {
const countries = api.getCountry();
res.send({
status: "ok",
data: countries
});
});
app.get("/athletes", (req, res, next) => {
const country = req.query.country;
if (country) {
const athletesByCountry = api.getAllAthleteByCountry(country);
res.send({
status: "ok",
data: athletesByCountry
});
} else {
const athletes = api.getAllAthlete();
res.send({
status: "ok",
data: athletes
});
}
});
app.get("/standings", asyncHandler(async(req, res, next) => {
const standings = await api.getStandings();
if (standings) {
res.send({
status: "ok",
data: standings
});
} else {
res.send({
status: "error"
});
}
}));
app.listen(PORT, () => console.log(`asian-games-api listening on port ${PORT}!`));