|
|
import Koa from 'koa'; |
|
|
import bodyParser from 'koa-bodyparser'; |
|
|
|
|
|
import combinedRouter from '@/controllers'; |
|
|
|
|
|
const app = new Koa(); |
|
|
const port = process.env.PORT || 3000; |
|
|
|
|
|
|
|
|
app.use(async (ctx, next) => { |
|
|
const start = Date.now(); |
|
|
await next(); |
|
|
const ms = Date.now() - start; |
|
|
console.log(`${ctx.method} ${ctx.url} - ${ctx.status} ${ms}ms`); |
|
|
}); |
|
|
|
|
|
|
|
|
app.use(bodyParser()); |
|
|
|
|
|
|
|
|
app.use(async (ctx, next) => { |
|
|
if (ctx.is('application/octet-stream')) { |
|
|
const buffers = []; |
|
|
for await (const chunk of ctx.req) { |
|
|
buffers.push(chunk); |
|
|
} |
|
|
ctx.request.body = Buffer.concat(buffers); |
|
|
} |
|
|
await next(); |
|
|
}); |
|
|
|
|
|
|
|
|
app.use(combinedRouter.routes()).use(combinedRouter.allowedMethods()); |
|
|
|
|
|
|
|
|
app.listen(port, () => { |
|
|
console.log(`Server running at http://localhost:${port}`); |
|
|
}); |
|
|
|