@tsed/platform-koa
Version:
Koa package for Ts.ED framework
30 lines (29 loc) • 840 B
JavaScript
import send from "koa-send";
/**
* @ignore
* @param options
*/
export function staticsMiddleware(options) {
if (options.index !== false)
options.index = options.index || "index.html";
return async (ctx, next) => {
if (["GET", "HEAD"].includes(ctx.method) && !ctx.body) {
let path = ctx.path;
if (ctx._matchedRoute && ctx._matchedRoute !== "/") {
path = path.replace(String(ctx._matchedRoute).replace("/(.*)", ""), "");
}
try {
const done = await send(ctx, path, options);
if (done) {
return;
}
}
catch (err) {
if (err.status !== 404) {
throw err;
}
}
}
return next();
};
}