app.js
1.62 KB
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
import Koa2 from 'koa'
import KoaBody from 'koa-body'
import KoaStatic from 'koa-static2'
import {
System as SystemConfig
} from './config'
import path from 'path'
import MainRoutes from './routes/main-routes'
import ErrorRoutesCatch from './middleware/ErrorRoutesCatch'
import ErrorRoutes from './routes/error-routes'
import jwt from 'koa-jwt'
import fs from 'fs'
var cors = require('koa2-cors')
// import PluginLoader from './lib/PluginLoader'
const app = new Koa2()
const env = process.env.NODE_ENV || 'development' // Current mode
const publicKey = fs.readFileSync(path.join(__dirname, '../publicKey.pub'))
app
.use(cors({ credentials: true, maxAge: 2592000 }))
.use(ErrorRoutesCatch())
.use(KoaStatic('assets', path.resolve(__dirname, '../assets'))) // Static resource
.use(jwt({ secret: publicKey }).unless({ path: [/^\/public|\/user\/login|\/assets|\/api\/wallpaper/] }))
.use(KoaBody({
multipart: true,
strict: false,
formidable: {
uploadDir: path.join(__dirname, '../assets/uploads/tmp')
},
jsonLimit: '10mb',
formLimit: '10mb',
textLimit: '10mb'
})) // Processing request
// .use(PluginLoader(SystemConfig.System_plugin_path))
.use(MainRoutes.routes())
.use(MainRoutes.allowedMethods())
.use(ErrorRoutes())
if (env === 'development') { // logger
app.use((ctx, next) => {
const start = new Date()
return next().then(() => {
const ms = new Date() - start
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)
})
})
}
app.listen(SystemConfig.API_server_port)
console.log('Now start API server on port ' + SystemConfig.API_server_port + '...')
export default app