-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
84 lines (67 loc) · 2.31 KB
/
index.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
83
84
const express = require('express')
const hbs = require('express-hbs')
const session = require('express-session')
const cookieParser = require('cookie-parser')
const MongoStore = require('connect-mongo')(session)
const timeout = require('connect-timeout')
const path = require('path')
const flash = require('connect-flash')
const config = require('config-lite')
const routes = require('./routes')
const apiRoutes = require('./routes/api')
const pkg = require('./package')
const bodyParser = require('body-parser')
const moment = require('moment')
const upload = require('./util/multerUtil')
const helpers = require('./helpers')
const app = express()
moment.locale('zh-cn')
app.disable('x-powered-by')
app.use(timeout('10s'))
app.use(bodyParser.json({
limit: '1mb'
}))
app.use(bodyParser.urlencoded({
extended: true,
limit: '1mb'
}))
app.engine('hbs', hbs.express4({
extname: '.hbs',
layoutsDir: path.join(__dirname, 'views', 'layout'),
defaultLayout: path.join(__dirname, 'views', 'layout', 'main.hbs')
}))
app.set('view engine', 'hbs')
app.set('views', path.join(__dirname, 'views'))
helpers.loadHelpers()
app.use(express.static(path.join(__dirname, 'public')))
app.use(cookieParser(config.session.secret))
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE')
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type, Authorization')
next()
})
// session 中间件
app.use(session({
name: config.session.key, // 设置 cookie 中保存 session id 的字段名称
secret: config.session.secret, // 通过设置 secret 来计算 hash 值并放在 cookie 中,使产生的 signedCookie 防篡改
cookie: {
maxAge: config.session.maxAge // 过期时间,过期后 cookie 中的 session id 自动删除
},
resave: false,
saveUninitialized: true,
store: new MongoStore({ // 将 session 存储到 mongodb
url: config.mongodb // mongodb 地址
})
}))
app.use(upload.single('files'))
// flash 中间件,用来显示通知
app.use(flash())
// 路由
app.use('/api/v1', apiRoutes)
// 路由
app.use('/', routes)
app.listen(config.port, function (req, res) {
console.log(`${pkg.name} started on http://localhost:${config.port} press Ctrl + C to terminate.`)
})
module.exports = app