|
|
@ -8,16 +8,14 @@ const morgan = require('koa-morgan') |
|
|
|
const compress = require('koa-compress') |
|
|
|
const streamLogStats = require('stream-log-stats') |
|
|
|
const cors = require('kcors') |
|
|
|
const conditional = require('koa-conditional-get'); |
|
|
|
const etag = require('koa-etag'); |
|
|
|
|
|
|
|
/** |
|
|
|
* @module local-web-server |
|
|
|
*/ |
|
|
|
module.exports = getApp |
|
|
|
|
|
|
|
process.on('unhandledRejection', (reason, p) => { |
|
|
|
throw reason |
|
|
|
}) |
|
|
|
|
|
|
|
function getApp (options) { |
|
|
|
options = Object.assign({ |
|
|
|
static: {}, |
|
|
@ -26,7 +24,6 @@ function getApp (options) { |
|
|
|
compress: false |
|
|
|
}, options) |
|
|
|
|
|
|
|
|
|
|
|
const log = options.log |
|
|
|
log.options = log.options || {} |
|
|
|
|
|
|
@ -35,6 +32,19 @@ function getApp (options) { |
|
|
|
/* CORS: allow from any origin */ |
|
|
|
app.use(convert(cors())) |
|
|
|
|
|
|
|
/* path blacklist */ |
|
|
|
app.use(function pathBlacklist (ctx, next) { |
|
|
|
if (/css$/.test(ctx.path)) { |
|
|
|
ctx.throw(403, 'FUCK NO.') |
|
|
|
} else { |
|
|
|
return next() |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
app.use(convert(conditional())) |
|
|
|
app.use(convert(etag())) |
|
|
|
|
|
|
|
/* mime-type overrides */ |
|
|
|
if (options.mime) { |
|
|
|
app.use((ctx, next) => { |
|
|
|
return next().then(() => { |
|
|
@ -47,6 +57,7 @@ function getApp (options) { |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
/* compress response */ |
|
|
|
if (options.compress) { |
|
|
|
app.use(convert(compress())) |
|
|
|
} |
|
|
@ -66,9 +77,12 @@ function getApp (options) { |
|
|
|
} |
|
|
|
if (log.format) app.use(convert(morgan.middleware(log.format, log.options))) |
|
|
|
|
|
|
|
/* serve static files */ |
|
|
|
if (options.static.root) { |
|
|
|
app.use(convert(serve(options.static.root, options.static.options))) |
|
|
|
} |
|
|
|
|
|
|
|
/* serve directory index */ |
|
|
|
if (options.serveIndex.path) { |
|
|
|
app.use(convert(serveIndex(options.serveIndex.path, options.serveIndex.options))) |
|
|
|
} |
|
|
@ -82,3 +96,7 @@ function logstalgiaDate () { |
|
|
|
.replace('GMT', '') |
|
|
|
.replace(' (BST)', '') |
|
|
|
} |
|
|
|
|
|
|
|
process.on('unhandledRejection', (reason, p) => { |
|
|
|
throw reason |
|
|
|
}) |