-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
120 lines (106 loc) · 3.28 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
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}
const express = require("express"),
path = require("path"),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
ejsMate = require("ejs-mate"),
session = require("express-session"),
MongoDBStore = require("connect-mongo")(session),
flash = require("connect-flash"),
mongoSanitize = require("express-mongo-sanitize"),
helmet = require("helmet"),
passport = require("passport"),
LocalStrategy = require("passport-local").Strategy,
methodOverride = require("method-override"),
User = require("./models/user"),
ExpressError = require("./utils/ExpressError");
const indexRoutes = require("./routes/index"),
campgroundsRoutes = require("./routes/campground"),
authRoutes = require("./routes/auth");
const store = new MongoDBStore({
url: process.env.DATABASEURL,
secret: process.env.SESSION_STORE_SECRET,
touchAfter: 24 * 3600,
});
store.on("error", function (e) {
console.log("Session Store Error: ", e);
});
const sessionConfig = {
name: "MongoSession",
store,
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: true,
secure: true,
expires: Date.now() + 604800000,
maxAge: 604800000,
},
};
mongoose.connect(process.env.DATABASEURL, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false,
});
const db = mongoose.connection;
db.on("error", console.error.bind(console, "Connection Error:"));
db.once("open", () => {
console.log("Database connected");
});
const app = express();
app.engine("ejs", ejsMate);
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "/views"));
app.use(express.static(path.join(__dirname, "public")));
app.use(methodOverride("_method"));
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.use(mongoSanitize({ replaceWith: "_" }));
if (process.env.NODE_ENV !== "production") {
const morgan = require("morgan");
app.use(morgan("tiny"));
}
app.use(session(sessionConfig));
app.use(flash());
app.use(helmet({ contentSecurityPolicy: false }));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(function (req, res, next) {
res.locals.currentUser = req.user;
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
next();
});
app.use(indexRoutes);
app.use(authRoutes);
app.use("/campgrounds", campgroundsRoutes);
/**
* TODO: Campgrounds Categories && sortby
* TODO: Mailing System
* TODO: Stats
*/
//** Error Handler
app.all("*", (req, res, next) => {
next(new ExpressError("Page Not Found", 404));
});
app.use((err, req, res, next) => {
const { statusCode = 500 } = err;
if (!err.message) err.message = "Oh No, Something Went Wrong!";
res.status(statusCode).render("error", { err });
});
//** Port
app.listen(process.env.PORT, function () {
console.log(
`Server is running on http://localhost:${process.env.PORT} in ${process.env.NODE_ENV} env`
);
});