Newer
Older
ServerGenerator / src / ExampleOutput / Middleware.ts
import express, { Request, Response, RequestHandler } from "express";
import * as DataAccess from "./DataAccess";
import { findSingle, toPayload } from "./Model";

export function graphMiddleware({ fileName }: { fileName: string }) {
    const graph = DataAccess.load(fileName);

    const route = express.Router();

    // TODO get/post collection, get/post/delete single entity
    route.get("/nodes/:id(\\d+)", (req, res) => {
        const id = Number.parseInt(req.param("id"), undefined);
        const node = findSingle(graph, id);
        if (node == null) {
            res.status(404).send("not found");
            return;
        }
        res.send(toPayload(node));
    });

    route.get("/nodes", (req, res) => {
        const nodes = graph.nodes;
        res.send(nodes.map(toPayload));
    });

    return route;
}