-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
100 lines (86 loc) · 3.19 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import bodyParser from "body-parser"
import dotenv from "dotenv"
import cors from "cors"
import express from "express"
import axios from "axios"
import mongoose from "mongoose"
import path, { dirname } from "path";
import { fileURLToPath } from 'url';
import matches from "./middleware/matches.js"
import login from "./routes/login.js"
import initLogin from "./middleware/initial_login.js"
import register from "./routes/register.js"
import save from "./routes/save.js"
import deleteAll from "./routes/delete.js"
import deleteSingle from "./routes/delete_single.js"
import headers from "./utils/setHeaders.js"
import tracker from "./middleware/tracker.js"
import swipe_left from "./routes/swipe_left.js"
import swipe_right from "./routes/swipe_right.js"
import seen from "./routes/seen.js"
dotenv.config()
const app = express()
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use(cors())
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
const path = require('path');
app.get('*', (req, res) => {
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
app.use(express.static(path.resolve(__dirname, "./client/build")));
}
);
}
// Set headers for the the requests
app.use(headers)
// Track all the requests in the console
app.use(tracker)
// Log the user in, and return a token
app.use("/api/login", login)
// Register a new user, save them to MongoDB, and return a token
app.use("/api/register", register)
// Save a new match the the database
app.use("/api/save_match", save)
// Delete all matches from the database
app.use("/api/delete_matches", deleteAll)
// Delete a single match from the database
app.use("/api/delete_one", deleteSingle)
// Get all matches for a user
app.use("/api/matches", matches)
// Recommend a new movie when the user swipes right
app.use("/api/swipe_right", swipe_right)
// Recommend a new movie when the user swipes left
app.use("/api/swipe_left", swipe_left)
// Add a movie to the seen list
app.use("/api/add_seen_movie", seen)
// Set their initial login status to false
app.use("/api/init_login", initLogin)
app.post("/api/get_movie", (req, res) => {
let id = Math.floor(Math.random() * 10) || Math.floor(Math.random() * 10)
let page = Math.floor((Math.random()*10) + 1)
axios.get(`https://api.themoviedb.org/3/discover/movie?api_key=${process.env.REACT_APP_TMDB_API_KEY}&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&page=${page}&with_watch_monetization_types=flatrate`
)
.then((response) => {
if (response.data.results[page].poster_path !== undefined) {
let random = Math.floor(Math.random() * response.data.results.length)
res.send(response.data.results[random]).status(200)
}
})
.catch((err) => {
res.send(err)
})
})
const uri = process.env.REACT_APP_URI
mongoose
.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log("MongoDB connection established..."))
.catch((error) => console.error("MongoDB connection failed:", error.message))
const PORT = process.env.REACT_APP_PORT
app.listen(PORT, () => {
console.log(`Server started on port ${PORT}`)
})