You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
842 B

  1. 'use strict'
  2. const Koa = require('koa')
  3. const serve = require('koa-static')
  4. const convert = require('koa-convert')
  5. const serveIndex = require('koa-serve-index')
  6. const morgan = require('koa-morgan')
  7. /**
  8. * @module local-web-server
  9. */
  10. module.exports = getApp
  11. process.on('unhandledRejection', (reason, p) => {
  12. throw reason
  13. })
  14. function getApp (options) {
  15. options = Object.assign({
  16. static: {},
  17. serveIndex: {},
  18. logger: {}
  19. }, options)
  20. const app = new Koa()
  21. if (options.logger.format) {
  22. app.use(convert(morgan.middleware(options.logger.format, options.logger.options)))
  23. }
  24. if (options.static.root) {
  25. app.use(convert(serve(options.static.root, options.static.options)))
  26. }
  27. if (options.serveIndex.path) {
  28. app.use(convert(serveIndex(options.serveIndex.path, options.serveIndex.options)))
  29. }
  30. return app
  31. }