frontpage/pages/api/projects/[name].ts

34 lines
1.0 KiB
TypeScript
Raw Normal View History

2022-01-16 14:58:48 +01:00
import { readFileSync } from "fs";
import { resolve } from "path";
import type { NextApiRequest, NextApiResponse } from "next";
interface IFileError {
message: string;
name: string;
stack?: string;
code: string;
errno: number;
syscall: string;
path: string;
}
export default function handler(req: NextApiRequest, res: NextApiResponse<string>) {
if (req.method !== "GET") return res.status(405).end();
if (!req.query.name) return res.status(400).end();
const project = req.query.name;
try {
const path = resolve("./public", "projects", `${project}.adoc`);
const data = readFileSync(path).toString();
2022-01-16 15:24:28 +01:00
console.debug(`[API/projects]\tRequest for ${project}`);
2022-01-16 14:58:48 +01:00
res.status(200).send(data);
} catch (err) {
2022-01-16 15:24:28 +01:00
console.error(`[API/projects]\tError in request for ${project}! Code: ${(err as IFileError).code}`);
2022-01-16 14:58:48 +01:00
if ((err as IFileError).code === "ENOENT") res.status(404);
else res.status(500);
} finally {
res.end();
}
}