This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
75 lines (58 loc) · 1.73 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
const path = require('path')
const express = require('express')
const mongoose = require('mongoose')
const dotenv = require('dotenv')
morgan = require('morgan')
const exphbs = require('express-handlebars')
const passport = require('passport')
const session = require('express-session')
// const MongoStore = require('connect-mongo')
const connectDB = require('./config/db')
// const { session } = require('passport')
// load config
dotenv.config({ path: './config/config.env' })
// passport config
require('./config/passport')(passport)
connectDB()
const app = express()
// Body parser
app.use(express.urlencoded({ extended: false }))
app.use(express.json())
// logging
if (process.env.NODE_ENV = 'development') {
app.use(morgan('dev'))
}
// hanflebars helpers
const { formatDate, stripTags, truncate, editIcon} = require('./helpers/hbs')
// handlebars
app.engine('.hbs', exphbs({ helpers: {
formatDate,
stripTags,
truncate,
editIcon,
} ,defaultLayout: 'main.hbs', extname: '.hbs' }));
app.set('view engine', '.hbs');
// sessions
app.use(
session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false,
// store: new MongoStore({ mongooseConnection: mongoose.connection })
}))
// passport middleware
app.use(passport.initialize())
app.use(passport.session())
// set global var
app.use(function(req,res, next) {
res.locals.user = req.user || null
next()
})
// static folder
app.use(express.static(path.join(__dirname, 'public')))
// routes
app.use('/', require('./routes/index'))
app.use('/auth', require('./routes/auth'))
app.use('/stories', require('./routes/stories'))
const PORT = process.env.PORT || 3000
app.listen(PORT, console.log(`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`))