-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
44 lines (36 loc) · 1.47 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
const express = require('express')
const { engine } = require('express-handlebars')
const methodOverride = require('method-override')
const session = require('express-session')
// const passport = require('passport')
const flash = require('connect-flash')
require('./config/mongoose')
const routes = require('./routes')
const passport = require('./config/passport')
const hbsHelpers = require('./helpers/hbsHelpers')
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const app = express()
const PORT = process.env.PORT || 3000
app.use(express.urlencoded({ extended: true }))
app.engine('hbs', engine({ defaultLayout: 'main', extname: 'hbs', helpers: hbsHelpers }))
app.set('view engine', 'hbs')//使用時省略寫副檔名
app.set('views', './views')//使用時省略./views
app.use(methodOverride('_method'));//RESTful API for PUT and DELET
app.use(express.static('public'))
app.use(flash())
app.use(session({ secret: process.env.SESSION_SECRET, resave: false, saveUninitialized: false }))
app.use(passport.initialize())
app.use(passport.session())
app.use((req, res, next) => {
res.locals.success_messages = req.flash('success_messages') // 設定 success_msg 訊息
res.locals.warning_messages = req.flash('warning_messages')
res.locals.error_messages = req.flash('error_messages') // 設定 warning_msg 訊息
res.locals.currentUser = req.user
next()
})
app.use(routes)
app.listen(PORT, () =>
console.log(`server is listening to http://localhost/${PORT}`)
)