-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
97 lines (76 loc) · 2.57 KB
/
server.js
File metadata and controls
97 lines (76 loc) · 2.57 KB
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
const path = require("path");
const express = require("express");
const cors = require("cors");
const hpp = require("hpp");
const compression = require("compression");
const { rateLimit } = require("express-rate-limit");
const dotenv = require("dotenv");
const morgan = require("morgan");
const mongoSanitize = require("express-mongo-sanitize");
const xss = require("xss-clean");
const { webhookCheckout } = require("./services/orderService");
// Load environment variables from 'config.env'
dotenv.config({
path: "config.env",
});
const databaseConnection = require("./config/database");
const AppError = require("./utils/appError");
const globalError = require("./middlewars/errorMiddleware");
// Import route modules
const mounteRoute = require("./routes/index");
// Connect to the database
databaseConnection();
// Create an instance of the Express application
const app = express();
//enable other domain to access your application
app.use(cors());
app.options("*", cors());
// compress all responses
app.use(compression());
//checkout session completed
app.post(
"/webhook-checkout",
express.raw({ type: "application/json" }),
webhookCheckout
);
// Middleware setup
app.use(express.json({ limit: "20kb" }));
// Allow access to static files in the "uploads" directory
app.use(express.static(path.join(__dirname, "uploads")));
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
// To apply data sanitization
app.use(mongoSanitize());
app.use(xss());
// Limit each Ip to 100 requests per 'window' (here, per 15 mintues)
const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000,
limit: 100,
message:
"Too many accounts created from this IP, please try again after an hour",
});
app.use("/api/", apiLimiter);
//middleware to protect against HTTP Parameter Pollution attacks
app.use(hpp());
// Mount Routes
mounteRoute(app);
// Handle unmatched routes
app.all("*", (req, res, next) => {
next(new AppError(`can't find this route:${req.originalUrl}`, 400));
});
// Global error handling middleware for express
app.use(globalError);
// Set up server to listen on specified port
const port = process.env.PORT || 8000;
const server = app.listen(port, () => {
console.log(`server running in port ${port}`);
});
// Handle unhandled rejections outside of Express
process.on("unhandledRejection", (err) => {
console.error(`unhandledRejection Errors: ${err.name} | ${err.message}`);
server.close(() => {
console.log("Shutting down...");
process.exit(1);
});
});