-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
67 lines (55 loc) · 1.88 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
import express from "express";
import flash from "express-flash";
import session from "express-session"
import passport from "passport";
import cookieParser from 'cookie-parser';
import bodyParser from "body-parser";
import path, {dirname} from "path";
import {fileURLToPath} from "url";
import methodOverride from "method-override";
import AuthMiddleware from "./middlewares/auth.js";
import PageUtil from "./utils/page.js";
import PassportUtil from "./utils/passport.js";
import authRoute from "./routes/auth.js";
import setUserMiddleware from "./middlewares/user.js";
import { Server } from "socket.io";
import SocketUtil from "./utils/socket.js";
import 'dotenv/config';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
PassportUtil.initialize(passport)
const app = express();
const port = process.env.APP_PORT;
const server = app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});
const io = new Server(server, {
cors: {
origin: [`localhost:${port}`, `127.0.0.1:${port}`]
},
});
SocketUtil.initialize(io);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
}))
app.use(cookieParser());
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(passport.initialize());
app.use(passport.session({}));
app.use(methodOverride('_method'));
app.use(flash());
app.use(setUserMiddleware);
app.get("/", (_req, res) => PageUtil.render(res, "index"));
app.get("/chat", AuthMiddleware.checkAuth, (_req, res) => PageUtil.render(res, "chat"));
app.get("/about-us", (_req, res) => PageUtil.render(res, "about-us"));
app.use(authRoute);
app.get('*', (_req, res) => {
res.status(404);
PageUtil.render(res, "utils/404");
});