/
index.js
49 lines (39 loc) · 1.1 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
const Koa = require('koa')
const config = require('./config')
const router = require("./routes")
const bodyParser = require('koa-bodyparser')
const parameter = require('koa-parameter')
const cors = require('koa2-cors')
// const routerResponse = require('./middleware/routerResponse')
const app = new Koa()
app.use(parameter(app))
// 不这样用会报错
app.use(bodyParser())
// app.use(cors({
// origin: 'localhost:4000'
// }))
app.use(async (ctx, next) => {
ctx.set('Access-Control-Allow-Origin', ctx.headers.origin)
ctx.set('Access-Control-Allow-Headers', 'content-type')
ctx.set('Access-Control-Allow-Methods', 'OPTIONS,GET,HEAD,PUT,POST,DELETE,PATCH')
ctx.set('Access-Control-Allow-Credentials', 'true')
await next()
})
// app.use(cors({
// origin: config.origin
// }))
// app.use(routerResponse())
app.use(async (ctx, next) => {
try {
await next()
} catch (err) {
ctx.status = err.status || err.statusCode || 500
console.log(err.message)
ctx.body = {
message: err.message
}
}
})
router(app)
app.listen(config.port)
console.log(`listening on port ${config.port}`)