import { dirname } from "node:path"; import { fileURLToPath } from "node:url"; import compress from "@universal-middleware/compress/fastify"; import { createHandler } from "@universal-middleware/fastify"; import { sendBigFile } from "@universal-middleware/tests/utils-node"; import handler from "@universal-middleware-examples/tool/dummy-handler-fastify"; import contextMiddleware from "@universal-middleware-examples/tool/middlewares/context-middleware-fastify"; import headersMiddleware from "@universal-middleware-examples/tool/middlewares/headers-middleware-fastify"; import paramsHandler from "@universal-middleware-examples/tool/params-handler-fastify"; import fastify from "fastify"; import rawBody from "fastify-raw-body"; import { args } from "./utils"; const _dirname = typeof __dirname !== "undefined" ? __dirname : dirname(fileURLToPath(import.meta.url)); const app = fastify(); // Optional: universal middlewares and handlers can read the request body without it. // When registered, they get the exact bytes the client sent rather than the body re-encoded from what Fastify parsed. await app.register(rawBody); // Now the universal context contains `{ hello: "World!!!" }`. // See /examples/context-middleware app.register(contextMiddleware("World!!!")); // After a Response has been returned by the handler below, // the `{ "X-Universal-Hello": "World!!!" }` header is appended to it app.register(headersMiddleware()); app.register(compress()); app.get("/user/:name", paramsHandler()); app.get("/big-file", createHandler(sendBigFile)()); app.get("/", handler()); const port = args.port ? Number.parseInt(args.port, 10) : 3000; app.listen( { port, host: "localhost", }, () => { console.log(`Server listening on http://localhost:${port}`); }, );