forked from ALPHACamp/twitter-api-2020
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
58 lines (45 loc) · 1.63 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
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const express = require('express')
const bodyParser = require('body-parser')
const flash = require('connect-flash')
const session = require('express-session')
const passport = require('./config/passport')
const methodOverride = require('method-override')
const helpers = require('./_helpers')
const cors = require('cors')
const db = require('./models')
const app = express()
const port = process.env.PORT
app.use(cors())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use(session({ secret: 'secret', resave: false, saveUninitialized: false }))
app.use(flash())
app.use(passport.initialize())
app.use(passport.session())
app.use(methodOverride('_method'))
app.use('/upload', express.static(__dirname + '/upload'))
app.use((req, res, next) => {
res.locals.success_messages = req.flash('success_messages')
res.locals.error_messages = req.flash('error_messages')
res.locals.user = helpers.getUser(req)
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Credentials', true)
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
res.header(
'Access-Control-Allow-Headers',
'Origin,X-Requested-With,Content-Type,Accept,content-type,application/json'
)
next()
})
app.get('/', (req, res) => res.send('Hello World!'))
require('./routes')(app)
// 加入 socket.io 監聽
const server = require('http').Server(app).listen(port, () => {
console.log(`The app is listening on port ${port}`)
})
const SocketSession = require('./socket')
SocketSession(server)
module.exports = app