web.js 876 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. 'use strict';
  2. const path = require('path');
  3. const Socket = require('ee-core').Socket;
  4. const Koa = Socket.Koa;
  5. const koaStatic = require('koa-static');
  6. const koaRouter = require('koa-router');
  7. /**
  8. * todo 浏览器访问
  9. */
  10. module.exports = {
  11. /**
  12. * 安装
  13. */
  14. install (eeApp) {
  15. eeApp.logger.info('[preload] load web module');
  16. const staticDir = path.join(eeApp.config.homeDir, 'public', 'dist');
  17. const koaApp = new Koa();
  18. koaApp.use(koaStatic(staticDir));
  19. const port = 7071;
  20. let url = 'http://127.0.0.1:' + port;
  21. // 路由
  22. const router = new koaRouter();
  23. router.all('/', async (ctx) => {
  24. ctx.type = 'json';
  25. ctx.body = '<h1>hello world!</h1>';
  26. })
  27. koaApp.use(router.routes());
  28. koaApp.listen(port, () => {
  29. // 服务创建成功
  30. eeApp.logger.info("web server:", url );
  31. });
  32. }
  33. }