-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
124 lines (104 loc) · 3.13 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
const express = require("express"),
app = express(),
mongoose = require("mongoose"),
session = require("express-session"),
passport = require("passport"),
multer = require("multer"),
uid = require("uid"),
path = require("path"),
sanitizer = require("express-sanitizer"),
methodOverride = require("method-override"),
localStrategy = require("passport-local"),
MongoStore = require("connect-mongodb-session")(session),
flash = require("connect-flash"),
User = require("./models/user"),
userRoutes = require("./routes/users"),
adminRoutes = require("./routes/admin"),
bookRoutes = require("./routes/books"),
authRoutes = require("./routes/auth");
// const Seed = require('./seed');
// uncomment below line for first time to seed database;
// Seed(1000);
// if (process.env.NODE_ENV !== "production") require("dotenv").config();
require("dotenv").config();
// app config
app.set("view engine", "ejs");
app.use(methodOverride("_method"));
app.use(express.static(__dirname + "/public"));
app.use(express.json());
app.use(express.urlencoded({extended: true}));
// app.use(bodyParser.urlencoded({ extended: true }));
app.use(sanitizer());
// db config
mongoose
.connect(process.env.DB_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
})
.then(() => console.log("MongoDB is connected"))
.catch((error) => console.log(error));
//PASSPORT CONFIGURATION
const store = new MongoStore({
uri: process.env.DB_URL,
collection: "sessions",
});
app.use(
session({
//must be declared before passport session and initialize method
secret: process.env.SESSION_SECRET,
saveUninitialized: false,
resave: false,
store,
})
);
app.use(flash());
app.use(passport.initialize()); //must declared before passport.session()
app.use(passport.session());
passport.use(new localStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
// configure image file storage
const fileStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "images");
},
filename: (req, file, cb) => {
cb(null, `${uid()}-${file.originalname}`);
},
});
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);
}
};
app.use(
multer({ storage: fileStorage, fileFilter: filefilter }).single("image")
);
app.use("/images", express.static(path.join(__dirname, "images")));
app.use((req, res, next) => {
res.locals.currentUser = req.user;
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
res.locals.warning = req.flash("warning");
next();
});
//Routes
app.use(userRoutes);
app.use(adminRoutes);
app.use(bookRoutes);
app.use(authRoutes);
app.listen(process.env.PORT, function(){
console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});
// const PORT = process.env.PORT || 3000;
// app.listen(PORT, () => {
// console.log(`server is running`);
// });