-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
95 lines (88 loc) · 2.72 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
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
import express, { Response, Request, NextFunction } from "express";
import routes from "./routes";
import dotenv from "dotenv";
import process from "process";
import cors from "cors";
import cookieParser from "cookie-parser";
import bodyParser from "body-parser";
import helmet from "helmet";
import csrf from "csurf";
import morgan from "morgan";
import { limiter } from "./configs/config.redis";
const whiteList = ["http://localhost:3000", "https://lads.vercel.app"];
dotenv.config();
const app = express();
const PORT = process.env.PORT || 8000;
/**
* All middlewares are added here, Configurations required for the project to work like json parsing, .env , etc.
*/
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser()); // Parses cookies
app.use(helmet()); // Helmet helps you secure your Express apps by setting various HTTP headers.
app.use(express.json()); // Parses JSON bodies
app.use(
cors({
credentials: true,
origin: whiteList,
optionsSuccessStatus: 200, // some legacy browsers (IE11, various SmartTVs) choke on 204
})
);
app.use(
csrf({
cookie: {
path: "/",
sameSite: "none",
secure: true,
httpOnly: true,
},
})
); // CSRF protection
app.use(morgan("tiny")); // Logs the requests
app.use(limiter); // Rate limiter
// CSRF Error Handler
app.use(function (err: any, req: Request, res: Response, next: NextFunction) {
if (err.code !== "EBADCSRFTOKEN") return next(err);
// handle CSRF token errors here
res.status(403);
res.json({ error: "Form tampered" });
return;
});
/**
* All routes are added here to get more information why aren't the routes shown here
* go to routes/index.ts for more information.
*/
app.use(routes);
//Instantiating the server
// We might use this sometime else but for now we will be relying on node balancing.
// if (cluster.isPrimary) {
// for (let i = 0; i < threads; i++) {
// cluster.fork();
// }
// cluster.on("exit", (worker, code, signal) => {
// console.log(
// `Worker ${worker.process.pid} died. Code number: ${code}. Signal: ${signal}`
// );
// cluster.fork();
// });
// } else {
// app.listen(PORT, () => {
// console.log(
// `Server is running on port http://localhost:${PORT} and is handled by process ${process.pid}`
// );
// });
// }
//test uncaughtexpections
process.on("uncaughtException", (err) => {
console.log(err);
console.log("shutting down due to uncaught exception");
process.exit(1);
});
app.listen(PORT, () => {
console.log(`Server is running on port http://localhost:${PORT}`);
});
//test unhandledrejections
process.on("unhandledRejection", (err) => {
console.log(err);
console.log("shutting down the server due to unhandled promise rejection");
process.exit(1);
});