-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
143 lines (119 loc) · 3.57 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
140
141
142
143
const path = require("path");
const fs = require("fs");
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const session = require("express-session");
const MongoDbStore = require("connect-mongodb-session")(session);
const csrf = require("csurf");
const flash = require("connect-flash");
const multer = require("multer");
const { v4: uuidv4 } = require("uuid");
require("dotenv").config();
const cloudinary = require("cloudinary").v2;
const helmet = require("helmet");
const compression = require("compression");
const morgan = require("morgan");
const adminRoutes = require("./routes/adminRoutes");
const shopRoutes = require("./routes/shopRoutes");
const authRoutes = require("./routes/auth");
const errorController = require("./controllers/errorController");
const MONGODB_URL = `mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@cluster0.temth.mongodb.net/${process.env.MONGO_DATABASE}?retryWrites=true&w=majority`;
const PORT = process.env.PORT || 8080;
const app = express();
app.use(express.static(path.join(__dirname, "public")));
app.use(express.static(path.join(__dirname, "images")));
// app.use(helmet());
app.use(compression());
const store = new MongoDbStore({
uri: MONGODB_URL,
collection: "sessions",
});
const csrfProtection = csrf();
//multer filters and config
const fileStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "images");
},
filename: (req, file, cb) => {
let fileUniqueName = uuidv4();
//get extension
let extensionArray = file.originalname.split(".");
let extension = extensionArray[extensionArray.length - 1];
cb(null, fileUniqueName + "." + extension);
},
});
const fileFilter = (req, file, cb) => {
if (
file.mimetype === "image/png" ||
file.mimetype === "image/jpg" ||
file.mimetype === "image/jpeg"
) {
cb(null, true);
} else {
cb(null, false);
}
};
//multer filters and config end
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(
multer({ storage: fileStorage, fileFilter: fileFilter }).single("image")
);
//cloudinary config
cloudinary.config({
cloud_name: `${process.env.CLOUDINARY_NAME}`,
api_key: `${process.env.CLOUDINARY_API_KEY}`,
api_secret: `${process.env.CLOUDINARY_API_SECRET}`,
});
//cloudinary config end
app.use(
session({
secret: "my secret",
resave: true,
saveUninitialized: true,
store: store,
})
);
app.use(csrfProtection);
app.use((req, res, next) => {
req.user = req.session.user;
next();
});
app.use((req, res, next) => {
res.locals.isAuthenticated = req.session.isLoggedIn;
res.locals.username = req.user ? req.user.name : undefined;
res.locals.csrfToken = req.csrfToken();
res.locals.role = req.user ? req.user.role : 2;
next();
});
app.use(flash());
app.use(authRoutes);
app.use("/admin", adminRoutes);
app.use(shopRoutes);
app.use("/500", errorController.error500);
app.all("*", (req, res, next) => {
let isLoggedIn = req.get("Cookie") ? req.get("Cookie").split("=")[1] : false;
isLoggedIn = Boolean(isLoggedIn);
res.render("404", {
title: "sorry page not found",
path: null,
isAuthenticated: isLoggedIn,
});
});
app.use((error, req, res, next) => {
res.redirect("/500");
});
mongoose
.connect(MONGODB_URL)
.then((result) => {
console.log("connected");
app.listen(PORT, () => {
console.log(`server running at ${process.env.HOST_URL}`);
});
})
.catch((err) => {
console.error(err);
res.redirect("/500");
});