-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (80 loc) · 2.34 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
85
86
87
88
89
90
91
92
93
94
const Koa = require('koa')
const path = require('path')
const bodyParser = require('koa-body')
const nunjucks = require('koa-nunjucks-2')
const serve = require('koa-static')
const config = require('config')
const fs = require('fs')
const chalk = require('chalk')
const cors = require('koa2-cors')
const shortid = require('shortid')
const initDb = require('./models/db')
const { mimeCollections } = require('./constants')
const { loadRoutes, requestLog, errorLog } = require('./utils')
const staticDirPath = '.' + path.resolve(__dirname, '/static')
const app = module.exports = new Koa()
if (!module.parent) {
// error log
app.use(requestLog)
// request log
app.use(errorLog)
}
// 指定静态资源目录
app.use(serve(staticDirPath))
// 模板引擎
app.use(nunjucks({
ext: 'html',
path: path.join(__dirname, 'views'),
nunjucksConfig: {
trimBlocks: true
}
}))
// 解析post请求
app.use(bodyParser({
formidable:{
uploadDir: __dirname + '/static', // directory where files will be stored
keepExtensions: true,
onFileBegin(name, file) {
const {
path: filePath,
type
} = file
const paths = filePath.split('/')
const suffixReg = /^(.*)(\..*)$/
const suffix = suffixReg.exec(paths[paths.length -1])[2]
let dirName
if (mimeCollections.imgType.includes(type)) {
dirName = 'imgs'
} else if (mimeCollections.musicType.includes(type)) {
dirName = 'music'
} else {
return null
}
file.path = path.resolve(
paths.slice(0, -1).join('/'),
dirName, `${shortid.generate()}${suffix}`
)
}
},
multipart: true,
urlencoded: true
}))
// cors config
app.use(cors())
// routes
loadRoutes(app)
// connect db
initDb()
.on('error', console.log)
.on('disconnected', initDb)
.on('open', listen)
function listen() {
if (!module.parent) {
const hostname = config.get('host.hostname')
const port = config.get('host.port')
app.listen(port, () => {
console.log(chalk.green(`server is running at ${hostname}:${port}`))
console.log('env:' + process.env.NODE_ENV)
})
}
}