Files
CPLv4.0/pages/api/cpl/readKnotenpunktAPIHandler.ts

42 lines
1.2 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// pages/api/cpl/readKnotenpunktAPIHandler.ts
import fs from "fs";
import path from "path";
import { NextApiRequest, NextApiResponse } from "next";
/**
* API-Handler, um eine Knotenpunkt-Mockdatei für einen bestimmten Slot (031) als JavaScript zu laden.
* Diese Datei wird im Frontend als <script src="..."> eingebunden.
*/
export default function handler(req: NextApiRequest, res: NextApiResponse) {
const { slot } = req.query;
const slotIndex = Number(slot);
// Sicherheitsprüfung
if (isNaN(slotIndex) || slotIndex < 0 || slotIndex > 31) {
return res.status(400).send("// ❌ Ungültiger Slot-Index");
}
const filePath = path.resolve(
process.cwd(),
`device-cgi-simulator/SERVICE/knotenpunkte/slot${slotIndex}.js`
);
// Datei vorhanden?
if (!fs.existsSync(filePath)) {
return res
.status(404)
.send(`// ❌ Datei slot${slotIndex}.js nicht gefunden`);
}
try {
const fileContent = fs.readFileSync(filePath, "utf8");
res.setHeader("Content-Type", "application/javascript");
res.status(200).send(fileContent);
} catch (error) {
console.error("Fehler beim Lesen der Knotenpunkt-Datei:", error);
res.status(500).send("// ❌ Fehler beim Lesen der Datei");
}
}