-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
66 lines (46 loc) · 2.02 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
const path = require('path')
const express = require('express')
const dotenv = require('dotenv')
const morgan = require('morgan')
const exphbs = require('express-handlebars')
const mongoose = require('mongoose')
const passport = require('passport')
const session = require('express-session')
// when we refresh the page or restart the server then the app auto logsout the user. to prevent this we have to store session in mongodb. we use connect-mongo pakage for it. it must be below session pakage as we pass it in connect-mongo. then configure session in session middleware below
const MongoStore = require('connect-mongo')(session)
const connectDB = require('./config/db')
// load config
dotenv.config({
path: './config/config.env'
})
// passport config
require('./config/passport')(passport)
connectDB()
const app = express()
// logging in console about http requests info
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'))
}
// setting up .hbs extension for handlebars, and defining default layout to be main.hbs file
app.engine('hbs', exphbs({ defaultLayout: 'main',extname: '.hbs' }))
app.set('view engine', '.hbs')
// static folder
app.use(express.static(path.join(__dirname, 'public')))
// session middleware. this must be above passport middleware
app.use(session({
secret: 'keyboard cat',
resave: false, // don't save session if nothing is modified
saveUninitialized: false, // don't create session until something is stored
store: new MongoStore({
mongooseConnection: mongoose.connection
}) // this creates a new collection named session in db and stores session for logged in user. if we log out then if destroys the session for that user
}))
// passport middleware
app.use(passport.initialize())
app.use(passport.session())
// routes
app.use('/', require('./routes/index'))
app.use('/auth', require('./routes/auth'))
app.use((req, res) => { res.render('error/404') })
const PORT = process.env.PORT || 3000
app.listen(PORT, console.log(`server running in ${process.env.NODE_ENV} mode on port ${PORT}`))