-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
41 lines (37 loc) · 1.3 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
const express = require('express')
const app = express()
const exphbs = require('express-handlebars')
require('./config/mongoose')
const bodyParser = require('body-parser')
const methodOverride = require('method-override')
const flash = require('connect-flash')
const session = require('express-session')
const usePassport = require('./config/passport')
const routes = require('./routes')
const PORT = process.env.PORT
// Template engine setting
app.engine('handlebars', exphbs({ defaultLayout: 'main' }))
app.set('view engine', 'handlebars')
app.use(express.static('public'))
// Body-parser setting
app.use(bodyParser.urlencoded({ extended: true }))
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true
}))
app.use(methodOverride('_method'))
app.use(flash())
usePassport(app) // 呼叫Passport函式並傳入app
app.use((req, res, next) => {
res.locals.isAuthenticated = req.isAuthenticated()
res.locals.user = req.user
res.locals.success_msg = req.flash('success_msg') // 設定 success_msg 訊息
res.locals.warning_msg = req.flash('warning_msg') // 設定 warning_msg 訊息
res.locals.error_msg = req.flash('error') // 設定 error_msg 訊息
next()
})
app.use(routes)
app.listen(PORT, () => {
console.log(`The express is listening on http://localhost:${PORT}`)
})