-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
121 lines (99 loc) · 2.96 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require('dotenv').config({ path: "./.env"})
const express = require("express");
const session = require('express-session');
const path = __dirname + '/app/views/dist/';
const cors = require("cors");
const app = express();
const http = require("http").createServer(app);
const bodyParser = require('body-parser');
const io = require("socket.io")(http, {
cors:{
origin:"http://localhost:5173"
}
});
const db = require("./app/models");
const history = require('connect-history-api-fallback');
const cookieParser = require("cookie-parser");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(history());
app.use(cookieParser());
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true
}));
// views
app.use(express.static(path));
var allowList = [
"http://localhost:5173",
"http://localhost:5000",
"http://localhost:8080",
"https://knowledge-club.herokuapp.com",
"https://knowledge-club-staging.herokuapp.com",
"http://yedia.io",
"https://yedia.io",
"http://www.yedia.io",
"https://www.yedia.io",
"https://knowledge-club-book-adder.herokuapp.com/"
]
//cors
var corsOptions = {
credentials: true
};
var corsOptionsDelegate = function (req, callback) {
if(allowList.indexOf(req.header("Origin")) > -1){
corsOptions = {
...corsOptions,
origin: true
};
}
else {
corsOptions = {
...corsOptions,
origin: false
}
}
callback(null, corsOptions);
};
app.use(cors(corsOptionsDelegate));
// parse requests of content-type - application/json
app.use(express.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));
// Routes
require('./app/routes/auth.routes')(app);
require('./app/routes/user.routes')(app);
require('./app/routes/data.routes')(app);
require('./app/routes/chat.routes')(app);
// controllers
const userController = require("./app/controllers/user.controller");
const roleController = require("./app/controllers/role.controller");
// DEVELOPMENT MODE
const initDb = async () => {
const userRole = await roleController.create({ id: 1, name: "user" })
const moderatorRole = await roleController.create({ id: 2, name: "moderator" })
const adminRole = await roleController.create({ id: 3, name: "admin" })
const adminUser = await userController.create({
username: process.env.ADMIN_USERNAME,
password: process.env.ADMIN_PASSWORD,
email: process.env.ADMIN_EMAIL
})
userController.setRole(adminUser.id, adminRole.id);
};
//database
// db.sequelize.sync({ force: true }).then(() => {
// console.log("Drop and re-sync db.");
// initDb();
// });
//END OF DEVELOPMENT MODE
// simple route
app.get("/", (req, res) => {
res.sendFile(path + "index.html");
});
// set port, listen for requests
const PORT = process.env.PORT || 8080;
http.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});
require("./socket-server")(io);