-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
executable file
·118 lines (105 loc) · 3.33 KB
/
server.ts
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
114
115
116
117
118
// REQUIRED MODULES
import express, { Application, Request, Response, NextFunction } from "express";
import methodOverride from "method-override";
import morgan from "morgan";
import session from "express-session";
import favicon from "serve-favicon";
import * as path from "path";
import * as mysql from "mysql2";
import MySQLStore from "express-mysql-session";
import * as dotenv from 'dotenv';
// PROCESS .ENV FILE
dotenv.config();
const PORT: number = parseInt(process.env.PORT || "3001");
const MYSQL_HOST: string = process.env.MYSQL_HOST;
const MYSQL_USER: string = process.env.MYSQL_USER;
const MYSQL_PASSWORD: string = process.env.MYSQL_PASSWORD;
const MYSQL_DATABASE: string = process.env.MYSQL_DATABASE;
// CONNECT MYSQL
const connection = mysql.createConnection({
host: MYSQL_HOST,
user: MYSQL_USER,
password: MYSQL_PASSWORD,
database: MYSQL_DATABASE,
});
// Middleware
const app: Application = express();
app.use(express.urlencoded({ extended: true }));
app.use(methodOverride("_method"));
app.use(express.static("public"));
app.use(morgan("dev"));
app.use(favicon(path.join(__dirname, "public", "images", "inventIcon.png")));
app.use(function (req: Request, res: Response, next: NextFunction) {
req.date = new Date().toLocaleDateString();
req.time = new Date().toLocaleTimeString();
next();
});
const sessionMiddleware = session({
cookie: {
secure: true,
maxAge: 60000,
},
store: new MySQLStore({
host: MYSQL_HOST,
user: MYSQL_USER,
password: MYSQL_PASSWORD,
database: MYSQL_DATABASE,
}),
secret: "supersecret",
saveUninitialized: true,
resave: false,
});
const csrfMiddleware = (req, res, next) => {
// Skip CSRF check for GET requests
if (req.method === "GET") {
return next();
}
const csrfToken = req.headers["x-csrf-token"] || req.body["_csrf"];
if (!csrfToken) {
return res.status(403).send("Invalid CSRF token");
}
// Verify the CSRF token
if (csrfToken !== req.session.csrfToken) {
return res.status(403).send("Invalid CSRF token");
}
next();
};
// Set the CSRF token in the session
app.use((req, res, next) => {
const csrfToken = req.session.csrfToken || Math.random().toString(36).slice(2);
req.session.csrfToken = csrfToken;
res.locals.csrfToken = csrfToken;
next();
});
// Add the custom CSRF middleware
app.use(csrfMiddleware);
// Add session middleware
app.use(sessionMiddleware);
// SET VIEW ENGINE
app.set("view engine", "ejs");
// CONTROLLERS
import itemsController from "./controllers/items";
app.use("/items", itemsController);
import usersController from "./controllers/users";
app.use("/users", usersController);
// ROUTING
app.get("/", (req: Request, res: Response) => {
res.redirect("users/login");
});
// wildcard route
// app.get("*", (req: Request, res: Response) => {
// res.redirect("/");
// });
// // HOW MANY TIMES VISITED
// app.get('/times-visited', function(req, res) {
// if(req.session.visits) {
// req.session.visits++;
// } else {
// req.session.visits = 1;
// };
// res.send(`<h1>You've visited this page ${req.session.visits} time(s) </h1>`);
// });
// Web server:
app.listen(PORT, () => {
console.log(`listening on port ${PORT}`);
});