-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
113 lines (91 loc) · 2.56 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
require("dotenv").config();
const express = require("express");
const morgan = require("morgan");
const session = require("express-session");
//const csurf = require('csurf');
const cookieParser = require("cookie-parser");
const flash = require("express-flash");
const helmet = require("helmet");
const sequelize = require("./config/db");
const cors = require("cors");
const socketIo = require("socket.io");
const http = require("http");
//const { errorHandler } = require('./middlewares/errorHandler');
const app = express();
const server = http.createServer(app);
//csrf
// const csrfMiddleware = csurf({
// cookie: true
// });
const corsOptions = {
origin: ["http://localhost:3000", "https://socializard-client.herokuapp.com"],
optionsSuccessStatus: 200,
credentials: true,
};
const run = async () => {};
sequelize.sequelize.sync({ force: false }).then(() => {
console.log("Drop and re-sync db.");
run();
});
app.use(cors(corsOptions));
app.use(express.static("public"));
app.set("view engine", "ejs");
app.use(morgan("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(
session({
secret: "super secret",
resave: false,
saveUninitialized: false,
cookie: {
sameSite: "none",
secure: true,
maxAge: 1000 * 60 * 60 * 24,
},
})
);
app.use(cookieParser());
//app.use(csrfMiddleware);
app.use(flash());
app.use(helmet());
// app.use((req, res, next) => {
// res.locals.csrfToken = req.csrfToken();
// next();
// });
const postRoutes = require("./routes/post.route");
const authRoutes = require("./routes/auth.route");
const friendsRoute = require("./routes/friends.route");
const messagesRoute = require("./routes/message.route");
const commentsRoute = require("./routes/comments.route");
const profileRoute = require("./routes/profile.route");
const userRoute = require("./routes/user.route");
app.use(profileRoute);
app.use(postRoutes);
app.use(authRoutes);
app.use(friendsRoute);
app.use(messagesRoute);
app.use(commentsRoute);
app.use(userRoute);
//socket.io
const io = (path) =>
require("socket.io")(server, {
cors: {
origin: [
"http://localhost:3000",
"https://socializard-client.herokuapp.com",
],
},
path,
});
const inboxIo = io("/inbox");
const { messagesSocket } = require("./controllers/message.controller");
messagesSocket(inboxIo);
app.use((req, res, next) => {
res.send("Route not found!");
});
//app.use(errorHandler);
const PORT = process.env.PORT || 8000;
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});