-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
81 lines (72 loc) · 2.37 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
/* eslint-disable camelcase */
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const usersRoute = require('./routers/users');
dotenv.config();
const passport = require('passport');
const User = require('./models/user');
const localStrategy = require('passport-local');
const authRoutes = require('./routers/auth');
const campgroundRoutes = require('./routers/campgrounds');
const commentRoutes = require('./routers/comments');
const connectFlash = require('connect-flash');
const methodOverride = require('method-override');
const session = require('express-session');
mongoose.connect(
process.env.DATABASEURL, {
useNewUrlParser: true
}
);
app.use(connectFlash());
// -----------------------------Express-session----------------------
const MongoStore = require('connect-mongo')(session);
app.use(
session({
secret: 'something special',
store: new MongoStore({
mongooseConnection: mongoose.connection
}),
resave: false,
saveUninitialized: false
})
);
app.use(methodOverride('_method'));
// ------------------------------Passport Session--------------------
app.use(passport.initialize());
app.use(passport.session());
const body_parser = require('body-parser');
app.set('view engine', 'ejs');
app.use(express.static(`${__dirname}/public`));
app.use((req, res, next) => {
// eslint-disable-next-line no-param-reassign
res.locals.user = req.user;
// eslint-disable-next-line no-param-reassign
res.locals.error = req.flash('error');
// eslint-disable-next-line no-param-reassign
res.locals.success = req.flash('success');
// eslint-disable-next-line no-param-reassign
res.locals.github = 'https://github.com/abhagsain';
next();
});
// ----------------------------- Passport Config --------------------------------
passport.use(new localStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
// ------------------------- Passport Config done! -----------------------------
app.use(
body_parser.urlencoded({
extended: true
})
);
app.use(authRoutes);
app.use(campgroundRoutes);
app.use(commentRoutes);
app.use(usersRoute);
app.get('*', (req, res) => {
res.render('notfound');
});
app.listen(process.env.PORT || 5000, process.env.IP, () => {
console.log('Server started! at PORT', process.env.PORT);
});