-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
94 lines (75 loc) · 2.13 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//External Import
const express = require("express");
const morgan = require("morgan");
const dotenv = require("dotenv");
const multer = require("multer");
const path = require("path");
const app = new express();
//Internal Import
const {
DefaultErrorHandler,
NotFoundError,
} = require("./src/helper/ErrorHandler");
//Confiqure dotenv
dotenv.config();
//Import Database Confiq
const connectDB = require("./src/confiq/db");
//Import route
const routes = require("./src/routes");
//Security Import
const cors = require("cors");
const hpp = require("hpp");
const helmet = require("helmet");
const rateLimit = require("express-rate-limit");
const expressMongoSanitize = require("express-mongo-sanitize");
const xssClean = require("xss-clean");
//Security middleware emplement
app.use(cors());
app.use(hpp());
app.use(
helmet.contentSecurityPolicy({
useDefaults: true,
directives: {
"img-src": ["'self'", "https: data: blob:"],
},
}),
);
app.use(expressMongoSanitize());
app.use(xssClean());
//Default middleware emplement
app.use(express.json({ limit: "50mb" }));
app.use(express.urlencoded({ extended: true }));
app.use(morgan("dev"));
// Apply the rate limiting middleware to all requests
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 10000,
standardHeaders: true,
legacyHeaders: false,
});
app.use(limiter);
const MONGODB_CONNECTION_URL = process.env.MONGODB_CONNECTION_URL;
const DB_OPTIONS = {
user: process.env.MONGODB_DATABASE_USERNAME,
pass: process.env.MONGODB_DATABASE_PASSWORD,
dbName: process.env.MONGODB_DATABASE_NAME,
autoIndex: true,
};
//connection database
connectDB(MONGODB_CONNECTION_URL, DB_OPTIONS);
// Routing Implement
app.use("/api/v1", routes);
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
// Add React Front End Routing
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
//static file
app.use("/", express.static(path.join(__dirname, "public")));
//Not Found Error Handler
app.use(NotFoundError);
// Default Error Handler
app.use(DefaultErrorHandler);
module.exports = app;