-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
52 lines (46 loc) · 1.5 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
const Koa = require('koa')
const app = new Koa()
const koaBody = require('koa-body')
const jwt = require('koa-jwt')
const koaStatic = require('koa-static')
const https = require('https')
const fs = require('fs')
const path = require('path')
const { ERROR_TYPE } = require('./constans')
const router = require('./router')
const sequelize = require('./model/sequelize')
app
.use((ctx, next) => {
return next().catch(err => {
if (err.status === 401) {
ctx.status = 200
ctx.body = {
error_code: ERROR_TYPE.AUTH_FAILED.code,
error_message: ERROR_TYPE.AUTH_FAILED.message
}
} else {
throw err
}
})
})
// 跨域
.use(async (ctx, next) => {
ctx.set('Access-Control-Allow-Origin', ctx.header.origin)
ctx.set('Access-Control-Allow-Credentials', true)
ctx.set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
ctx.set('Access-Control-Allow-Headers', 'X-Real-IP, Content-Type, Authorization')
await next()
})
.use(koaStatic(path.join(__dirname, 'public')))
.use(koaBody({ multipart: true }))
.use(router.routes())
.use(router.allowedMethods())
const server = require('http').createServer(app.callback())
server.listen(6000, '0.0.0.0')
sequelize.sync()
// if using https
// const httpsOption = {
// key: fs.readFileSync('/etc/letsencrypt/live/xxx.com/privkey.pem'),
// cert: fs.readFileSync('/etc/letsencrypt/live/xxx.com/fullchain.pem')
// }
// https.createServer(httpsOption, app.callback()).listen(6001, '0.0.0.0')