@digicms/cms
Version:
An open source headless CMS solution to create and manage your own API. It provides a powerful dashboard and features to make your life easier. Databases supported: MySQL, MariaDB, PostgreSQL, SQLite
24 lines (19 loc) • 584 B
JavaScript
;
const path = require('path');
const koaStatic = require('koa-static');
// serveStatic is not supposed to be used to serve a folder that have sub-folders
const serveStatic = (filesDir, koaStaticOptions = {}) => {
const serve = koaStatic(filesDir, koaStaticOptions);
return async (ctx, next) => {
const prev = ctx.path;
const newPath = path.basename(ctx.path);
ctx.path = newPath;
await serve(ctx, async () => {
ctx.path = prev;
await next();
ctx.path = newPath;
});
ctx.path = prev;
};
};
module.exports = serveStatic;