-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
51 lines (44 loc) · 1.45 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
//載入 express 並建構應用程式伺服器
const express = require('express')
const session = require('express-session')
const exphbs = require('express-handlebars')
const bodyParser = require('body-parser')
const methodOverride = require('method-override')
const routes = require('./routes')
const Todo = require('./models/todo')
const usePassport = require('./config/passport')
const flash = require('connect-flash')
//新增判斷:若NODE_ENV不在production狀態(正式上線模式)就引入我們設定的環境變數
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
require('./config/mongoose')
const app = express()
const PORT = process.env.PORT
console.log(process.env.PORT)
//設定樣板引擎
app.engine('hbs', exphbs({ defaultLayout: 'main', extname: '.hbs' }))
app.set('view engine', 'hbs')
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: 'false',
saveUnInitialized: true,
})
)
app.use(bodyParser.urlencoded({ extended: true }))
app.use(methodOverride('_method'))
usePassport(app)
app.use(flash())
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 訊息
next()
})
app.use(routes)
//設定port
app.listen(PORT, () => {
console.log(`App is running on http://localhost:${PORT}`)
})