-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
48 lines (41 loc) · 1.11 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
const Koa = require('koa')
const app = new Koa()
const http = require('http')
const json = require('koa-json')
const bodyparser = require('koa-bodyparser')
const logger = require('koa-logger')
const mongoosePaginate = require('mongoose-paginate')
const config = require('./config')
const router = require('./routes')
const mongodb = require('./db')
const interceptorMiddle = require('./middlewares/interceptor')
mongodb.connect()
mongoosePaginate.paginate.options = {
limit: config.MONGODB.limit
}
app.use(interceptorMiddle)
app.use(bodyparser({
enableTypes:['json', 'form', 'text']
}))
app.use(json())
app.use(logger())
/* app.use(async (ctx, next) => {
try {
await next()
} catch (error) {
ctx.body = {
code: 0,
message: 'koa服务器内部出错了!'
}
if (ctx.status === 404 || ctx.status === 405) {
ctx.body = {
code: 0,
message: '调用api无效'
}
}
}
}) */
app.use(router.routes(), router.allowedMethods())
http.createServer(app.callback()).listen(config.SERVER.port, () => {
console.log(`server run at http://localhost:${config.SERVER.port}`)
})