forked from ALPHACamp/twitter-fullstack-2020
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
45 lines (39 loc) · 1.52 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
const express = require('express')
const app = express()
const server = require('http').createServer(app)
const hbs = require('express-handlebars')
const bodyParser = require('body-parser')
const session = require('express-session')
const methodOverride = require('method-override')
const flash = require('connect-flash')
const helpers = require('./_helpers')
const useSocketIO = require('./config/socketIO')
const passport = require('./config/passport')
const useHbsHelper = require('./config/hbs-helpers')
const useRoutes = require('./routes')
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const { PORT } = process.env
app.engine('hbs', hbs({ defaultLayout: 'main', extname: 'hbs', helpers: useHbsHelper }))
app.set('view engine', 'hbs')
app.use(express.static('public'))
app.use(session({ secret: process.env.SESSION_SECRET, resave: false, saveUninitialized: false }))
app.use(bodyParser.urlencoded({ extended: true }))
app.use(methodOverride('_method'))
app.use(flash())
app.use(passport.initialize())
app.use(passport.session())
useSocketIO(server)
app.use((req, res, next) => {
res.locals.isAuth = helpers.ensureAuthenticated(req)
res.locals.user = helpers.getUser(req)
res.locals.success_messages = req.flash('success_messages')
res.locals.error_messages = req.flash('error_messages')
next()
})
// 顯示上傳的圖片
app.use('/upload', express.static(__dirname + '/upload'))
server.listen(PORT, () => console.log(`The server is running on http://localhost:${PORT}`))
useRoutes(app)
module.exports = app