nfp_sites/api/serve.mjs

32 lines
801 B
JavaScript
Raw Normal View History

2019-02-19 11:34:52 +00:00
import send from 'koa-send'
import defaults from './defaults'
2019-02-19 11:34:52 +00:00
export function serve(docRoot, pathname, options = {}) {
options.root = docRoot
return (ctx, next) => {
let opts = defaults({}, options)
2019-02-19 11:34:52 +00:00
if (ctx.request.method === 'OPTIONS') return
let filepath = ctx.path.replace(pathname, '')
if (filepath === '/') {
filepath = '/index.html'
}
if (filepath.endsWith('.jpg')
|| filepath.endsWith('.png')
|| filepath.endsWith('.js')
|| filepath.endsWith('.css')
|| filepath.endsWith('.svg')) {
opts = defaults({ maxage: 2592000 * 1000 }, opts)
}
return send(ctx, filepath, opts).catch((er) => {
2019-02-19 11:34:52 +00:00
if (er.code === 'ENOENT' && er.status === 404) {
return send(ctx, '/index.html', options)
}
})
}
}