-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
107 lines (92 loc) · 2.8 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
require("dotenv").config();
const createError = require("http-errors");
const express = require("express");
const engine = require("ejs-mate");
const path = require("path");
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
const logger = require("morgan");
const passport = require("passport");
const User = require("./models/user");
const session = require("express-session");
const mongoose = require("mongoose");
const methodOverride = require("method-override");
// const seedPosts = require('./seeds');
// seedPosts();
// require routes
const indexRouter = require("./routes/index");
const postsRouter = require("./routes/posts");
const reviewsRouter = require("./routes/reviews");
const app = express();
// connect to database
mongoose.connect(
"mongodb://localhost:27017/surf-shop",
{
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
},
function () {
console.log("we are connected to database");
}
);
// use ejs-locals for all ejs templates:
app.engine("ejs", engine);
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.use(logger("dev"));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
app.use(methodOverride("_method"));
// Configure passport and sessions
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
})
);
app.use(passport.initialize());
app.use(passport.session());
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
// set local variables middleware
app.use(function (req, res, next) {
// req.user = {
// '_id' : '5ec1ed3613730335b86b27af',
// '_id' : '5fce9189806a840164ea8f7a',
// '_id' : '5fcfeb37dd50382c344107a7',
// 'username' : 'aayush'
// }
res.locals.currentUser = req.user;
res.locals.title = "Surf Shop";
res.locals.success = req.session.success || "";
delete req.session.success;
res.locals.error = req.session.error || "";
delete req.session.error;
next();
});
// Mount Routes
app.use("/", indexRouter);
app.use("/posts", postsRouter);
app.use("/posts/:id/reviews", reviewsRouter);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
// error handler
app.use(function (err, req, res, next) {
// // set locals, only providing error in development
// res.locals.message = err.message;
// res.locals.error = req.app.get('env') === 'development' ? err : {};
// // render the error page
// res.status(err.status || 500);
// res.render('error');
console.log(err);
req.session.error = err.message;
res.redirect("back");
});
module.exports = app;