|
@ -57,8 +57,10 @@ function localWebServer (options) { |
|
|
const convert = require('koa-convert') |
|
|
const convert = require('koa-convert') |
|
|
const cors = require('kcors') |
|
|
const cors = require('kcors') |
|
|
const _ = require('koa-route') |
|
|
const _ = require('koa-route') |
|
|
|
|
|
const json = require('koa-json') |
|
|
pathToRegexp = require('path-to-regexp') |
|
|
pathToRegexp = require('path-to-regexp') |
|
|
debug = require('debug')('local-web-server') |
|
|
debug = require('debug')('local-web-server') |
|
|
|
|
|
const bodyParser = require('koa-bodyparser') |
|
|
|
|
|
|
|
|
const log = options.log |
|
|
const log = options.log |
|
|
log.options = log.options || {} |
|
|
log.options = log.options || {} |
|
@ -81,6 +83,12 @@ function localWebServer (options) { |
|
|
/* CORS: allow from any origin */ |
|
|
/* CORS: allow from any origin */ |
|
|
app.use(cors()) |
|
|
app.use(cors()) |
|
|
|
|
|
|
|
|
|
|
|
/* pretty print JSON */ |
|
|
|
|
|
app.use(json()) |
|
|
|
|
|
|
|
|
|
|
|
/* request body parser */ |
|
|
|
|
|
app.use(bodyParser()) |
|
|
|
|
|
|
|
|
/* rewrite rules */ |
|
|
/* rewrite rules */ |
|
|
if (options.rewrite && options.rewrite.length) { |
|
|
if (options.rewrite && options.rewrite.length) { |
|
|
options.rewrite.forEach(route => { |
|
|
options.rewrite.forEach(route => { |
|
@ -155,14 +163,12 @@ function localWebServer (options) { |
|
|
/* serve static files */ |
|
|
/* serve static files */ |
|
|
if (options.static.root) { |
|
|
if (options.static.root) { |
|
|
const serve = require('koa-static') |
|
|
const serve = require('koa-static') |
|
|
// verbose('static', 'enabled')
|
|
|
|
|
|
app.use(serve(options.static.root, options.static.options)) |
|
|
app.use(serve(options.static.root, options.static.options)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/* serve directory index */ |
|
|
/* serve directory index */ |
|
|
if (options.serveIndex.path) { |
|
|
if (options.serveIndex.path) { |
|
|
const serveIndex = require('koa-serve-index') |
|
|
const serveIndex = require('koa-serve-index') |
|
|
// verbose('serve-index', 'enabled')
|
|
|
|
|
|
app.use(serveIndex(options.serveIndex.path, options.serveIndex.options)) |
|
|
app.use(serveIndex(options.serveIndex.path, options.serveIndex.options)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -266,8 +272,8 @@ function mockResponses (options) { |
|
|
|
|
|
|
|
|
if (mock) { |
|
|
if (mock) { |
|
|
Object.assign(ctx.response, mockedReponse) |
|
|
Object.assign(ctx.response, mockedReponse) |
|
|
options.verbose('mocked response', JSON.stringify(mockedReponse)) |
|
|
|
|
|
options.verbose('actual response', JSON.stringify(ctx.response)) |
|
|
|
|
|
|
|
|
// options.verbose('mocked response', JSON.stringify(mockedReponse))
|
|
|
|
|
|
// options.verbose('actual response', JSON.stringify(ctx.response))
|
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
return next() |
|
|
return next() |
|
|