-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (62 loc) · 2.29 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const http = require("http"),
express = require("express"),
SocketIO = require("socket.io"),
bodyParser = require("body-parser"),
path = require("path"),
sequelize = require("./db"),
cors = require("cors"),
allowCors = require("./utils/allowCors"),
app = express();
const PORT = process.env.PORT || 5000;
const server = http.Server(app);
// Bodyparser Middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Settings for CORS
let allowedOrigins = [process.env.CLIENT_URL, "http://localhost:3000"];
// allowCors(app);
app.use(cors({ origin: allowedOrigins }));
const io = SocketIO.listen(server);
const runSocket = require("./socket");
const User = require("./models/user");
const Chat = require("./models/chat");
const Friend = require("./models/friend");
const Message = require("./models/message");
const Blocked = require("./models/blocked");
const Unread = require("./models/unread");
const Request = require("./models/request");
// const UserChats = require("./models/userChats");
// const UserFriends = require("./models/userFriends");
// const UserBlockeds = require("./models/userBlockeds");
// const UserRequests = require("./models/userRequests");
const chat = require("./api/chat");
const user = require("./api/user");
const request = require("./api/request");
Chat.hasMany(Message);
Message.belongsTo(Chat);
Chat.hasMany(Unread);
Unread.belongsTo(Chat);
User.hasMany(Unread);
Unread.belongsTo(User);
User.belongsToMany(Chat, { through: "UserChat" });
Chat.belongsToMany(User, { through: "UserChat" });
Friend.belongsToMany(User, { through: "UserFriend" });
User.belongsToMany(Friend, { through: "UserFriend" });
Blocked.belongsToMany(User, { through: "UserBlocked" });
User.belongsToMany(Blocked, { through: "UserBlocked" });
Request.belongsToMany(User, { through: "UserRequest" });
User.belongsToMany(Request, { through: "UserRequest" });
sequelize.sync().catch(err => console.log(err));
app.use("/api/chats", chat);
app.use("/api/users", user);
app.use("/api/requests", request);
runSocket(io);
// Server static assets if in production
if (process.env.NODE_ENV === "production") {
// Set static folder
app.use(express.static("index.html"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "index.html"));
});
}
server.listen(PORT);