-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
63 lines (49 loc) · 1.65 KB
/
Copy pathserver.js
File metadata and controls
63 lines (49 loc) · 1.65 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
const path = require("path");
const express = require("express");
const dotenv = require("dotenv");
const morgan = require("morgan");
const cors = require("cors");
const compression = require("compression");
dotenv.config({ path: "config.env" });
const dbConnection = require("./config/database");
const ApiError = require("./utils/apiError");
const globalError = require("./middlewares/errorMiddleware");
// Mount Routes
const mountRoutes = require("./routes");
const { webhookCheckout } = require("./services/orderService");
//Connect with DB
dbConnection();
//Express apps
const app = express();
//Allow other domains to access the application
app.use(cors());
app.options("*", cors());
//Compress all responses
app.use(compression());
//Checkout webhook
app.post("/webhook-checkout", express.raw({ type: "application/json" }), webhookCheckout);
//Middlewares
app.use(express.json());
app.use(express.static(path.join(__dirname, "uploads")));
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
console.log(`Mode: ${process.env.NODE_ENV}`);
}
mountRoutes(app);
app.all("*", (req, res, next) => {
next(new ApiError(`Can't find this route: ${req.originalUrl}`, 400));
});
// Global error handling middleware for express
app.use(globalError);
const PORT = process.env.PORT || 8000;
const server = app.listen(PORT, () => {
console.log(`App running on port: ${PORT}`);
});
// Handle rejections outside express
process.on("unhandledRejection", (err) => {
console.error(`UnhandledRejection error: ${err.name} | ${err.message}`);
server.close(() => {
console.error("Sutting server down...");
process.exit(1);
});
});