-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
168 lines (157 loc) · 4.1 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const path = require("path");
const session = require("express-session");
const MongoStore = require("connect-mongo")(session);
const flash = require("connect-flash");
const multer = require("multer");
const csrf = require("csurf");
const compression = require("compression");
const imageMin = require("imagemin");
const imageMinWebp = require("imagemin-webp");
const aws = require("aws-sdk");
const movieRoute = require("./routes/movie");
const userRoute = require("./routes/user");
const User = require("./model/user");
const errorController = require("./controllers/error");
const utils = require("./utils/utils");
const app = express();
const sessionStore = new MongoStore({
url: ` mongodb+srv://saifibne:${process.env.MONGODB_PASSWORD}@cluster0.5up7i.mongodb.net/movie?retryWrites=true&w=majority`,
collection: "sessions",
mongoOptions: {
useNewUrlParser: true,
useUnifiedTopology: true,
},
});
aws.config.update({
accessKeyId: process.env.AWS_KEY,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
});
const s3 = new aws.S3();
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "./images");
},
filename: (req, file, cb) => {
cb(null, new Date().toISOString() + "-" + file.originalname);
},
});
const fileFilter = (req, file, cb) => {
if (
file.mimetype === "image/jpeg" ||
file.mimetype === "image/jpg" ||
file.mimetype === "image/png" ||
file.mimetype === "image/webp"
) {
cb(null, true);
} else {
cb(null, false);
}
};
app.set("view engine", "pug");
app.set("views", "./views");
app.use(express.static(path.join(__dirname, "shared")));
app.use(
"/build/images",
express.static(path.join(__dirname, "build", "images"))
);
app.use(
"/fontawesome",
express.static(
path.join(__dirname, "node_modules", "@fortawesome", "fontawesome-free")
)
);
app.use(compression());
app.use(
multer({ storage: storage, fileFilter: fileFilter }).single("imageUrl")
);
app.use(async (req, res, next) => {
if (req.file) {
const imageFile = await imageMin([req.file.path], {
destination: "build/images",
plugins: [imageMinWebp()],
});
const image = imageFile[0];
const imageNameArray = image.destinationPath.split("/");
const imageName = imageNameArray[imageNameArray.length - 1];
const params = {
Bucket: "test-bucket-5577",
Key: imageName,
Body: image.data,
};
s3.upload(params, (error, data) => {
if (error) {
throw error;
} else {
req.uploadData = data;
utils.deleteImage(image.destinationPath);
next();
}
});
} else {
next();
}
});
app.use(bodyParser.urlencoded({ extended: false }));
app.use(
session({
secret: "superSecret",
resave: false,
saveUninitialized: false,
store: sessionStore,
})
);
app.use(flash());
app.use(csrf());
app.use((req, res, next) => {
if (!req.session.user) {
return next();
}
const userId = req.session.user._id;
User.findOne({ _id: userId })
.then((user) => {
req.user = user;
next();
})
.catch((err) => {
console.log(err);
});
});
app.use((req, res, next) => {
if (!req.session) {
return next();
}
res.locals.isLoggedIn = req.session.isLoggedIn;
res.locals._csrf = req.csrfToken();
next();
});
app.use(movieRoute);
app.use("/admin", userRoute);
app.use(errorController.getErrorPage);
app.use((error, req, res, next) => {
if (error) {
const message = error.message;
const statusCode = error.statusCode || 500;
res
.status(404)
.sendFile(path.join(__dirname, "views", "error", "404.html"));
}
});
mongoose
.connect(
` mongodb+srv://saifibne:${process.env.MONGODB_PASSWORD}@cluster0.5up7i.mongodb.net/movie?retryWrites=true&w=majority`,
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
)
.then((result) => {
console.log("connected to database");
app.listen(process.env.PORT || 3000);
})
.catch((err) => {
console.log(err);
});