Add project api route

This commit is contained in:
Daniel Kluge 2022-01-13 18:15:32 +01:00
parent c137ed08f5
commit ee8e4b9fb9
2 changed files with 33 additions and 0 deletions

33
pages/api/get_project.ts Normal file
View File

@ -0,0 +1,33 @@
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.projectName) return res.status(400).end();
const project = req.query.projectName;
try {
const path = resolve("./public", "projects", `${project}.md`)
const data = readFileSync(path).toString();
console.debug(`[API/get_project]\tRequest for ${project}`);
res.status(200).send(data);
} catch (err) {
console.error(`[API/get_project]\tError in request for ${project}! Code: ${(err as IFileError).code}`);
if ((err as IFileError).code === "ENOENT") res.status(404);
else res.status(500);
} finally {
res.end();
}
}

0
public/projects/.gitkeep Normal file
View File