-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.mjs
103 lines (87 loc) · 2.78 KB
/
app.mjs
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
import "./config.mjs";
import "./models/Resume.mjs";
import "./models/Workspace.mjs";
import "./models/User.mjs";
// import "./models/Duration.mjs";
// import "./models/Experience.mjs";
// import "./models/Education.mjs";
// import "./models/SkillSlot.mjs";
import express from "express";
import session from "express-session";
import cors from "cors";
import path from "path";
import passport from "passport";
import passportConfig from "./passportConfig.mjs";
import { fileURLToPath } from "url";
import mongoose from "mongoose";
const app = express();
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// import routes
import dashboardRoutes from "./routes/dashboardRoutes.mjs";
import registrationRoutes from "./routes/registrationRoutes.mjs";
import loginRoutes from "./routes/loginRoutes.mjs";
import logoutRoutes from "./routes/logoutRoutes.mjs";
import workspaceRoutes from "./routes/workspaceRoutes.mjs";
import resumeRoutes from "./routes/resumeRoutes.mjs";
/// middleware
app.use(express.static(path.join(__dirname, "public")));
// cors
const corsOptions = {
origin: (process.env.NODE_ENV === 'production') ? process.env.PROD_FRONTEND_URL : process.env.DEV_FRONTEND_URL,
credentials: true,
optionsSuccessStatus: 200
};
app.use(cors(corsOptions));
console.log(corsOptions);
// body parser (req.body)
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
// session
const sessionOptions = {
secret: process.env.SESSION_SECRET,
saveUninitialized: false,
resave: false,
cookie: {
sameSite : "none",
secure: true,
domain: (process.env.NODE_ENV === 'production') ? process.env.PROD_FRONTEND_URL : process.env.DEV_FRONTEND_URL,
httpOnly: true,
maxAge: 1000 * 60 * 60,
secure: true,
}
};
app.use(session(sessionOptions));
// passport
app.use(passport.initialize());
app.use(passport.session());
// passport config
passportConfig();
// log requests
function logRequest(req, res, next) {
console.log(
`Method: ${req.method}\n` +
`Path: ${req.path}\n` +
`Query: ${JSON.stringify(req.query)}\n` +
`Body: ${JSON.stringify(req.body)}\n`
);
next();
}
app.use(logRequest);
// routes
app.use(dashboardRoutes);
app.use(registrationRoutes);
app.use(loginRoutes);
app.use(logoutRoutes);
app.use(workspaceRoutes);
app.use(resumeRoutes);
app.listen(((process.env.NODE_ENV === 'production') ? process.env.PORT : process.env.DEV_PORT) || 8080, () => {
mongoose
.connect((process.env.NODE_ENV === 'production') ? process.env.PROD_DSN : process.env.DEV_DSN)
.then(() => {
console.log("Successfully connected to MongoDB.");
// console.log(`DSN: ${(process.env.NODE_ENV === 'production') ? process.env.PROD_DSN : process.env.DEV_DSN}`);
})
.catch((error) => {
console.error("Connection error: ", error);
});
});