forked from jmcgaha0209/TopTier-Games
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
64 lines (51 loc) · 1.64 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
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const cors = require("cors");
const cookieParser = require('cookie-parser');
require("dotenv").config();
const path = require("path");
const PORT = process.env.PORT || 3001;
const uri = process.env.MONGODB_URL;
const usersRouter = require('./routes/userRouter');
const gamesRouter = require('./routes/gameRouter');
const progressRouter = require('./routes/progressRouter');
const rankingRouter = require('./routes/rankingRouter');
const refreshRouter = require('./routes/refreshRouter');
mongoose.connect(uri);
const app = express();
app.set("port", process.env.PORT || 3001);
const allowedOrigins = ['http://localhost:3000', "https://poosd-large-project-group-8-1502fa002270.herokuapp.com", 'https://www.toptier.games'];
const corsOptions = {
origin: function (origin, callback)
{
if (!origin || allowedOrigins.indexOf(origin) !== -1)
{
callback(null, true);
} else
{
callback(new Error('Not allowed by CORS'));
}
},
credentials: true,
};
app.use(cors(corsOptions));
app.use(bodyParser.json());
app.use(cookieParser());
if (process.env.NODE_ENV === "production")
{
app.use(express.static(path.join(__dirname, "frontend/build")));
app.get("*", (req, res) =>
{
res.sendFile(path.resolve(__dirname, "frontend", "build", "index.html"));
});
}
app.listen(PORT, () =>
{
console.log("Server listening on port " + PORT);
});
app.use('/Users', usersRouter);
app.use('/Games', gamesRouter);
app.use('/Progress', progressRouter);
app.use('/Ranking', rankingRouter);
app.use('/Refresh', refreshRouter);