-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
58 lines (50 loc) · 1.55 KB
/
index.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
import express, { type Express } from "express";
import userRoutes from "./src/routes/userRoutes.ts";
import db from "./src/config/db.ts";
import cookieParser from "cookie-parser";
import { doubleCsrf } from "csrf-csrf";
import propertyRoutes from "./src/routes/propertyRoutes.ts";
import appRoutes from "./src/routes/appRoutes";
import apiRoutes from "./src/routes/apiRoutes";
// App init
const app: Express = express();
const PORT: number = 3000;
const { doubleCsrfProtection } = doubleCsrf({
getSecret: () => process.env.CSRF_SECRET || "secret",
cookieName: process.env.COOKIE_NAME || "csrf-cookie",
cookieOptions: {
path: "/",
sameSite: "strict",
secure: false,
signed: true,
},
size: 32,
getTokenFromRequest: (req) => {
return req.body["csrfToken"] || req.headers["x-csrf-token"];
},
});
// DB connection
try {
await db.authenticate();
await db.sync();
} catch (error) {
console.log(error);
}
// Configure body
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser(process.env.CSRF_SECRET || "secret_key"));
app.use(doubleCsrfProtection);
// Configurations
app.set("view engine", "pug");
app.set("views", "./src/views");
// Public folder
app.use(express.static("./src/public"));
// Routing
app.use("/", doubleCsrfProtection, appRoutes);
app.use("/auth", doubleCsrfProtection, userRoutes);
app.use("/", doubleCsrfProtection, propertyRoutes);
app.use("/api", doubleCsrfProtection, apiRoutes);
// Server
app.listen(process.env.PORT || 3000, (): void => {
console.log(`Server running on http://localhost:${PORT}`);
});