-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.js
139 lines (124 loc) · 3.21 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Node Modules Imports
*/
const express = require("express");
const path = require("path");
const cors = require("cors");
const ejsMate = require("ejs-mate");
const methodOverride = require("method-override");
const morgan = require("morgan");
const coookieParser = require("cookie-parser");
const session = require("express-session");
const flash = require("connect-flash");
const mongoSanitize = require('express-mongo-sanitize');
const helmet = require("helmet");
/**
* Configs Imports.
*/
const { sessionConfig } = require("./configs/config");
/**
* Utils Imports
*/
const connectDb = require("./utils/connectDb");
const AppError = require("./utils/AppError");
const getCurrentUser = require("./utils/getCurrentUser");
/**
* Router imports
*/
const UserRouter = require("./routes/user/user.router");
const ProductRouter = require("./routes/product/product.router");
const ProductRouterV2 = require("./routes/product/v2/productv2.router");
const StripeRouter = require("./routes/stripe/stripe.router");
const WebhookRouter = require("./routes/stripe/webhook.router");
/**
* Declarations
*/
const app = express();
const PORT = process.env.PORT || 3000;
/**
* Middlewares
*/
// Listen for incoming webhook requests
app.use("/", WebhookRouter);
// ------------------------------------
app.engine("ejs", ejsMate);
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "/views"));
// Security Middlewares
app.use(mongoSanitize());
app.use(
helmet({
contentSecurityPolicy: false,
crossOriginEmbedderPolicy: false,
})
);
app.use(express.static(path.join(__dirname, "public")));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cors());
app.use(methodOverride('_method'));
app.use(coookieParser(process.env.SIGN_COOKIE));
app.use(morgan("dev"));
app.use(session(sessionConfig));
app.use(flash());
/**
* Setting global variables
*/
app.use(async (req, res, next)=>{
res.locals.currentUser = await getCurrentUser(req, res);
res.locals.success = req.flash("success");
res.locals.error = req.flash("error");
next();
});
/**
* Routes middleware
*/
app.use("/", UserRouter);
app.use("/", ProductRouter);
app.use("/", StripeRouter);
/**
* Routes middleware v2
*/
app.use("/", ProductRouterV2);
/**
* Home route.
*/
app.route("/").get((req, res) => {
res.render("home");
});
/**
* Check health route
*/
app.route("/health").get((req, res)=>{
return res.status(200).send({
status: "success",
message: "Server is up and running."
});
});
/**
* If none of the routes matches.
*/
app.all('*', (req, res, next)=>{
next(new AppError('This page does not exist or unavailable.', 404));
})
/**
* Default error handling middleware.
*/
app.use((err, req, res, next)=>{
const { status=500, message="Something went wrong", stack } = err;
res.render("error", {status, message });
})
const runServer = async () =>{
if(process.env.NODE_ENV !== "production"){
await connectDb();
app.listen(PORT, ()=>{
console.log(`🚀 Development server live on http://localhost:${PORT}`);
})
} else {
await connectDb(process.env.MONGODB_CONNECTION_STRING);
app.listen(PORT, ()=>{
console.log(`🚀 Production server live on port ${PORT}`);
})
}
}
runServer();