-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
152 lines (128 loc) · 4.42 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
const express = require("express");
const ejs = require("ejs");
const mongoose = require("mongoose");
const passport = require("passport");
const LocalStrategy = require("passport-local");
const passportLocalMongoose = require("passport-local-mongoose");
const bodyParser = require("body-parser");
const flash = require("req-flash");
const expressSession = require("express-session");
const authController = require("./controllers/auth.controller");
const middleWares = require("./middlewares/middlewares");
const cookieParser = require("cookie-parser");
const multer = require("multer");
const path = require("path");
const fs = require("fs");
const uuid = require("uuid/v4");
const compression = require("compression");
const helmet = require("helmet");
const User = require("./models/user.model");
//Creating server
const server = express();
//Setting view engine
server.set("view engine", "ejs");
server.use("/assets/css", express.static(__dirname + "/assets/css"));
server.use("/assets/img", express.static(__dirname + "/assets/img"));
server.use("/uploads", express.static(__dirname + "/uploads"));
server.use("/assets/js", express.static(__dirname + "/assets/js"));
//connect to mongo db
mongoose.connect(
"mongodb+srv://ajal:ajal123@e-xhibyte-3krkl.mongodb.net/test?retryWrites=true&w=majority",
{
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
}
);
//Using resources
server.use(
expressSession({
secret: "Polisaanam",
resave: false,
saveUninitialized: false,
})
);
server.use(bodyParser.urlencoded({ extended: false }));
server.use(bodyParser.json());
server.use(passport.initialize());
server.use(passport.session());
server.use(cookieParser());
server.use(flash());
server.use(compression()); //Compress all routes
server.use(helmet()); //Use Helmet to protect against well known vulnerabilities
//Setting up Passport
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
//Route Path
const adminRoute = require("./routes/admin.route");
const userRoute = require("./routes/user.route");
const bookExbRoute = require("./routes/bookExb.route");
const artExbRoute = require("./routes/artExb.route");
const productExbRoute = require("./routes/productExb.route");
//Routing
server.use("/polisaanam", middleWares.hasAdminPrivs, adminRoute);
server.use("/profile", middleWares.isLoggedIn, userRoute);
server.use("/bookExb", middleWares.isLoggedIn, bookExbRoute);
server.use("/artExb", middleWares.isLoggedIn, artExbRoute);
server.use("/productExb", middleWares.isLoggedIn, productExbRoute);
authController(server);
//Static pages
server.get("/", (req, res) => {
if (req.user) return res.render("home");
return res.render("index");
});
server.get("/about", (req, res) => {
res.render("about");
});
server.get("/home", middleWares.isLoggedIn, (req, res) => {
res.render("home", { user: req.user });
});
// @Ajal333, add func to remove dp.
//Uploading profile Image
//Path to save files
const upload = multer({ dest: __dirname + "/uploads/" });
server.get("/upload", (req, res) => {
res.render("profileImageUploader");
});
server.post(
"/upload",
middleWares.isLoggedIn,
upload.single("file" /* name attribute of <file> element in your form */),
(req, res) => {
const tempPath = req.file.path;
const imageName = uuid().toString() + ".png";
const targetPath = path.join(__dirname, "./uploads/" + imageName);
if (
path.extname(req.file.originalname).toLowerCase() === ".png" ||
path.extname(req.file.originalname).toLowerCase() === ".jpeg"
) {
fs.rename(tempPath, targetPath, (err) => {
if (err) return handleError(err, res);
User.findByIdAndUpdate(
{ _id: req.user.id },
{ imgUrl: imageName },
(err) => {
if (err) {
console.log(err);
return res.redirect("/profile");
}
console.log("Image added.");
return res.redirect("/profile");
}
);
});
} else {
fs.unlink(tempPath, (err) => {
if (err) return handleError(err, res);
res
.status(403)
.contentType("text/plain")
.end("Only .png and .jpeg files are allowed!");
});
}
}
);
// Profile Image Uploading done
const PORT = process.env.PORT || 5000;
server.listen(PORT, () => console.log(`Working on: http://localhost:${PORT}`));