-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
55 lines (41 loc) · 1.58 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
require("dotenv").config();
const cors = require("cors");
const express = require("express");
const path = require("path");
const app = express();
const PORT = process.env.PORT || 8081;
const { limiter } = require("./src/utils/rateLimiter");
const { sendInternalError } = require("./src/utils/sendRequest");
const {
getUserPreferences,
saveOrUpdateUserPreferences,
} = require("./src/routes/getOrSaveUserPreferences");
const getId = require("./src/routes/getId");
const getItems = require("./src/routes/getItems");
// Use CORS middleware
app.use(cors());
// Handle pre-flight requests
app.options("*", cors());
app.use(express.static(path.join(__dirname, "public")));
app.use(express.json());
/* A route that is used to get the data for all items. */
app.get("/", limiter, getItems);
/* A route that is used to get the data for a specific movie. */
app.get("/movie/:id", limiter, getId);
/* A route that is used to get the data for a specific tvshow. */
app.get("/tvshow/:id", limiter, getId);
/* A route to get user preferences */
app.get("/preferences/:email", getUserPreferences);
/* A route to save or update user preferences */
app.post("/preferences/:email", saveOrUpdateUserPreferences);
/* Catch-all route for invalid endpoints */
app.all("*", (req, res) => {
const error = new Error(
`Invalid endpoint: ${req.originalUrl}. Allowed endpoints are: '/', '/movie/:id', '/tvshow/:id'.`,
);
sendInternalError(res, error);
});
/* Starting the server on the port defined in the PORT variable. */
app.listen(PORT, () => {
console.log(`server started on http://localhost:${PORT}`);
});