koa-template/server.js

23 lines
575 B
JavaScript
Raw Normal View History

2019-12-18 02:59:34 +00:00
const Koa = require('koa')
const app = new Koa()
const config = require('@config/index.js')
const server = app.listen(config.server.port, () => {
console.log(`server start on port ${server.address().port}`)
})
module.exports = server
// load middleware module
const koaLogger = require('koa-logger')
const koaCors = require('@koa/cors')
const rootRouter = require('@route/index.js')
app.use(koaLogger())
app.use(koaCors({
credentials: true,
// allow all origin
origin: ctx => ctx.get('origin')
}))
app.use(rootRouter.allowedMethods())
app.use(rootRouter.routes())