|
@ -2,8 +2,8 @@ |
|
|
const Koa = require('koa') |
|
|
const Koa = require('koa') |
|
|
const serve = require('koa-static') |
|
|
const serve = require('koa-static') |
|
|
const convert = require('koa-convert') |
|
|
const convert = require('koa-convert') |
|
|
const extend = require('deep-extend') |
|
|
|
|
|
const serveIndex = require('koa-serve-index') |
|
|
const serveIndex = require('koa-serve-index') |
|
|
|
|
|
const morgan = require('koa-morgan') |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @module local-web-server |
|
|
* @module local-web-server |
|
@ -15,14 +15,22 @@ process.on('unhandledRejection', (reason, p) => { |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
function getApp (options) { |
|
|
function getApp (options) { |
|
|
options = extend({ |
|
|
|
|
|
static: { root: '.' }, |
|
|
|
|
|
serveIndex: { path: '.' } |
|
|
|
|
|
|
|
|
options = Object.assign({ |
|
|
|
|
|
static: {}, |
|
|
|
|
|
serveIndex: {}, |
|
|
|
|
|
logger: {} |
|
|
}, options) |
|
|
}, options) |
|
|
|
|
|
|
|
|
const app = new Koa() |
|
|
const app = new Koa() |
|
|
|
|
|
|
|
|
app.use(convert(serve(options.static.root, options.static.options))) |
|
|
|
|
|
app.use(convert(serveIndex(options.serveIndex.path, options.serveIndex.options))) |
|
|
|
|
|
|
|
|
if (options.logger.format) { |
|
|
|
|
|
app.use(convert(morgan.middleware(options.logger.format, options.logger.options))) |
|
|
|
|
|
} |
|
|
|
|
|
if (options.static.root) { |
|
|
|
|
|
app.use(convert(serve(options.static.root, options.static.options))) |
|
|
|
|
|
} |
|
|
|
|
|
if (options.serveIndex.path) { |
|
|
|
|
|
app.use(convert(serveIndex(options.serveIndex.path, options.serveIndex.options))) |
|
|
|
|
|
} |
|
|
return app |
|
|
return app |
|
|
} |
|
|
} |