-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
80 lines (66 loc) · 2 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
71
72
73
74
75
76
77
78
79
80
const express = require("express");
const fileUpload = require("express-fileupload");
const session = require("express-session");
const MySQLStore = require("express-mysql-session")(session);
const crypto = require("crypto");
const dotenv = require("dotenv");
const connection = require("./models/connection");
const flash = require("./middlewares/flash"); // Import flash middleware
dotenv.config();
// Import routes
const clientRoutes = require("./routes/clientRoutes/index");
const authRoutes = require("./routes/serverRoutes/index");
const app = express();
// Session configuration
const sessionSecret = process.env.SESSION_SECRET || "keyboard mouse";
const sessionStore = new MySQLStore(
{
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE,
port: process.env.MYSQL_PORT || 3306,
clearExpired: true,
checkExpirationInterval: 15 * 60 * 1000,
},
connection
);
const sessionConfig = {
secret: sessionSecret,
store: sessionStore,
cookie: {
maxAge: 24 * 60 * 60 * 1000,
httpOnly: true,
secure: process.env.NODE_ENV === "development" ? true : false,
},
resave: false,
saveUninitialized: false,
};
// Set up session middleware
app.use(session(sessionConfig));
// Set up flash middleware
app.use(flash);
// Set EJS as the view engine
app.set("view engine", "ejs");
app.set("views", "views");
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Serve static files from the 'public' directory
app.use(express.static("public"));
// Serve static files from the 'uploads' directory
app.use("/uploads", express.static("uploads"));
// Configure file upload middleware
app.use(
fileUpload({
createParentPath: true,
tempFileDir: "/temp",
})
);
// Define routes
app.use("/", clientRoutes);
app.use("/admin", authRoutes);
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});