index.js 643 B

12345678910111213141516171819202122
  1. import Koa from 'koa';
  2. import serve from 'koa-static';
  3. import config from './app.config.js';
  4. import router from './router/index.mjs';
  5. import { koaBody } from 'koa-body';
  6. const app = new Koa();
  7. // 托管静态文件
  8. app.use(serve(config.public));
  9. // 一定要在路由之前先处理请求体的转换
  10. // 默认对 json and xml and urlencoded 进行转换
  11. app.use(koaBody());
  12. // 添加路由
  13. app.use(router.routes());
  14. // 在options请求下响应支持的请求方法
  15. app.use(router.allowedMethods());
  16. app.listen(config.port, config.host, () => {
  17. console.info(`服务已启动,地址为:'http://${config.host}:${config.port}'.`);
  18. });