|
| 1 | +/* |
| 2 | + * WHAT IS THIS FILE? |
| 3 | + * |
| 4 | + * It's the entry point for the Fastify server when building for production. |
| 5 | + * |
| 6 | + * Learn more about Node.js server integrations here: |
| 7 | + * - https://qwik.dev/docs/deployments/node/ |
| 8 | + * |
| 9 | + */ |
| 10 | +import { type PlatformNode } from "@builder.io/qwik-city/middleware/node"; |
| 11 | +import "dotenv/config"; |
| 12 | +import Fastify from "fastify"; |
| 13 | +import { join } from "node:path"; |
| 14 | +import { fileURLToPath } from "node:url"; |
| 15 | +import FastifyQwik from "./plugins/fastify-qwik"; |
| 16 | + |
| 17 | +declare global { |
| 18 | + interface QwikCityPlatform extends PlatformNode {} |
| 19 | +} |
| 20 | + |
| 21 | +// Directories where the static assets are located |
| 22 | +const distDir = join(fileURLToPath(import.meta.url), "..", "..", "dist"); |
| 23 | +const buildDir = join(distDir, "build"); |
| 24 | + |
| 25 | +// Allow for dynamic port and host |
| 26 | +const PORT = parseInt(process.env.PORT ?? "3000"); |
| 27 | +const HOST = process.env.HOST ?? "0.0.0.0"; |
| 28 | + |
| 29 | +const start = async () => { |
| 30 | + // Create the fastify server |
| 31 | + // https://fastify.dev/docs/latest/Guides/Getting-Started/ |
| 32 | + const fastify = Fastify({ |
| 33 | + logger: true, |
| 34 | + }); |
| 35 | + |
| 36 | + // Enable compression |
| 37 | + // https://github.com/fastify/fastify-compress |
| 38 | + // IMPORTANT NOTE: THIS MUST BE REGISTERED BEFORE THE fastify-qwik PLUGIN |
| 39 | + // await fastify.register(import('@fastify/compress')) |
| 40 | + |
| 41 | + // Handle Qwik City using a plugin |
| 42 | + await fastify.register(FastifyQwik, { distDir, buildDir }); |
| 43 | + |
| 44 | + // Start the fastify server |
| 45 | + await fastify.listen({ port: PORT, host: HOST }); |
| 46 | +}; |
| 47 | + |
| 48 | +start(); |
0 commit comments