forked from Technigo/express-api-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
41 lines (33 loc) · 1.07 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
import express from "express";
// import bodyParser from "body-parser"
import cors from "cors";
import data from "./data.json";
// console.log(data.length);
// Defines the port the app will run on. Defaults to 8080, but can be overridden
// when starting the server. Example command to overwrite PORT env variable value:
// PORT=9000 npm start
const port = process.env.PORT || 8080;
const app = express();
// Add middlewares to enable cors and json body parsing
app.use(cors());
app.use(express.json());
// Start defining your routes here
app.get("/", (req, res) => {
res.send("Hello Technigo!");
});
app.get("/nominations", (req, res) => {
res.json(data)
});
app.get("/year/:year", (req, res) => {
const year = req.params.year
const showWon = req.query.won
let nominationsFromYear = data.filter((item) => item.year_award === +year)
if (showWon) {
nominationsFromYear = nominationsFromYear.filter((item) => item.win)
}
res.json(nominationsFromYear)
})
// Start the server
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});