-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (59 loc) · 1.73 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
//start of app.js
const { createServer } = require("http");
const { Server } = require("socket.io");
const express = require("express");
require("dotenv").config();
const helmet = require("helmet");
const path = require("path");
const cors = require("cors");
const sequelize = require("./util/database");
const User = require("./models/user");
const Message = require("./models/message");
const Group = require("./models/group");
const GroupMember = require("./models/group-member");
const socketService = require("./services/socket");
const cronService = require("./services/cronjob");
cronService.job.start();
const userRoutes = require("./routes/user");
const messageRoutes = require("./routes/message");
const pageRoutes = require("./routes/page");
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, "public")));
app.use(
helmet({
contentSecurityPolicy: false,
})
);
app.use("/user", userRoutes);
app.use("/message", messageRoutes);
app.use(pageRoutes);
const httpServer = createServer(app);
const io = new Server(httpServer);
io.on("connection", socketService);
User.hasMany(Message);
Message.belongsTo(User, {
constraints: true,
onDelete: "CASCADE",
foreignKey: "userId",
});
User.belongsToMany(Group, { through: GroupMember });
Group.belongsToMany(User, { through: GroupMember });
Group.belongsTo(User, {
foreignKey: "AdminId",
constraints: true,
onDelete: "CASCADE",
});
Group.hasMany(Message);
Message.belongsTo(Group);
const PORT = process.env.PORT;
sequelize
.sync()
.then(() => {
httpServer.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
})
.catch((err) => console.log(err));