mcfly/routes/[...index].ts
2023-10-07 22:45:45 +02:00

22 lines
546 B
TypeScript

import * as fs from "fs";
export default eventHandler((event) => {
const filename = event.path === "/" ? "/index.html" : `${event.path}.html`;
const fallback = getPath(event.path + "/index.html");
const path = getPath(filename);
let html = "";
try {
html = fs.readFileSync(path, "utf8");
} catch (error) {
try {
html = fs.readFileSync(fallback, "utf8");
} catch {
html = `cannot find ${path} or ${fallback}`;
}
}
return html;
});
function getPath(filename: string) {
return `./pages${filename}`;
}