-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.js
82 lines (63 loc) · 2.05 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
75
76
77
78
79
80
81
82
// app.js
const express = require('express')
const app = express()
// include dotenv when not in production mode
if (process.env.NODE_ENV !== 'production') { require('dotenv').config() }
const flash = require('connect-flash')
const exphbs = require('express-handlebars')
const bodyParser = require('body-parser')
const csrf = require('csurf')
const methodOverride = require('method-override')
const userRoutes = require('./routes/user')
const todoRoutes = require('./routes/todo')
const homeRoutes = require('./routes/home')
const authRoutes = require('./routes/auth')
const searchRoutes = require('./routes/search')
const passport = require('passport')
const session = require('express-session')
const errorController = require('./controllers/error')
// Initialize csrf protection middleware
const csrfProtection = csrf()
// Include models
const db = require('./models')
const Todo = db.Todo
const User = db.User
const port = 3000
app.engine('handlebars', exphbs({ defaultLayout: 'main' }))
app.set('view engine', 'handlebars')
app.use(bodyParser.urlencoded({ extended: true }))
app.use(methodOverride('_method'))
// setup session
app.use(session({
secret: 'kldxnflkdzsfrf',
resave: 'false',
saveUninitialized: 'false'
}))
// set up passport
app.use(passport.initialize())
app.use(passport.session())
require('./config/passport')(passport)
// set up connect-flash
app.use(flash())
// use csrf protection middleware after session
app.use(csrfProtection)
app.use((req, res, next) => {
res.locals.user = req.user
res.locals.reminder = req.flash('reminder')
res.locals.warning = req.flash('warning')
res.locals.success = req.flash('success')
// generate one CSRF token to every render page
res.locals.csrfToken = req.csrfToken()
next()
})
app.use(express.static('public'))
app.use('/todos', todoRoutes)
app.use('/users', userRoutes)
app.use('/auth', authRoutes)
app.use('/search', searchRoutes)
app.use('/', homeRoutes)
app.use(errorController.getError)
// 設定 express port 3000
app.listen(port, () => {
console.log(`App is running on port ${port}!`)
})