app.js 861 B

12345678910111213141516171819202122232425262728293031323334353637
  1. require('./app/conf/code.inc')
  2. const Koa = require('koa')
  3. const app = new Koa()
  4. const morgan = require('koa-morgan')
  5. const cookie = require('koa-cookie').default
  6. const bodyParser = require('koa-better-body')
  7. const router = require('./app/router')
  8. // setup the logger
  9. app.use(morgan('dev'))
  10. // setup router
  11. app.use(router.routes())
  12. router.use(cookie())
  13. // setup body parser
  14. app.use(bodyParser({
  15. multipart: true
  16. }))
  17. app.use(async (ctx, next) => {
  18. Object.assign(ctx.request.query, ctx.request.fields)
  19. await next()
  20. ctx.set('Access-Control-Allow-Origin', ctx.header.origin)
  21. ctx.set('Access-Control-Allow-Credentials', true)
  22. ctx.set('Access-Control-Allow-Methods', 'GET,HEAD,PUT,POST,DELETE')
  23. })
  24. // setup listen
  25. const PORT = process.env.PORT || 1024
  26. app.listen(PORT, () => {
  27. console.log(`App listening on port ${PORT}!`)
  28. })
  29. module.exports = app