-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
92 lines (83 loc) · 2.24 KB
/
server.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
const path = require("path");
const dotenv = require("dotenv");
dotenv.config({ path: "./config.env" });
const express = require("express");
const morgan = require("morgan");
const cors = require("cors");
const compression = require("compression");
const { rateLimit } = require("express-rate-limit");
const hpp = require("hpp");
const mongoSanitize = require("express-mongo-sanitize");
const xss = require("xss-clean");
//
const { dbConnection } = require("./config/db");
const ApiError = require("./utils/apiError");
const globalErrorHandler = require("./middlewares/errorHandling");
const mountRoutes = require("./routes");
const { webhookCheckout } = require("./controllers/order-controller");
// Database Connection
dbConnection();
//Express App
const app = express();
// Middlewares
app.use(cors());
app.options("*", cors()); // pre-flight request
app.use(compression());
app.post(
"/webhook-checkout",
express.raw({ type: "application/json" }),
webhookCheckout
);
app.use(
express.json({
limit: "15kb",
})
);
app.use(express.static(path.join(__dirname, "uploads")));
app.use(express.urlencoded({ extended: true }));
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
limit: 50,
message: "Too many requests , please try again in 15 minutes!",
});
app.use(mongoSanitize());
app.use(xss());
app.use("/api", limiter);
app.use(
hpp({
whitelist: [
"price",
"name",
"title",
"sold",
"quantity",
"ratingsAverage",
"ratingsQuantity",
"fields",
], // and so on
})
);
// Mounting Routes
mountRoutes(app);
// 404 error handler
app.all("*", (req, res, next) => {
next(new ApiError("Page not found!", 400));
});
// global error handler (express errors)
app.use(globalErrorHandler);
// running app
const port = process.env.PORT || 8000;
const server = app.listen(port, () => {
console.log(`Server up and running on port ${port}...`);
});
// unhandled rejections (outside express) (event -> listen -> callback fun)
process.on("unhandledRejection", (err) => {
console.error(`Unhandled Rejection Error : ${err.name} | ${err.message}`);
server.close(() => {
console.log("Shutting Server down....");
process.exit(1);
});
});