|
| 1 | +/* |
| 2 | + * WHAT IS THIS FILE? |
| 3 | + * |
| 4 | + * It's the entry point for the Express HTTP 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 { createQwikCity, type PlatformNode } from '@builder.io/qwik-city/middleware/node'; |
| 11 | +import 'dotenv/config'; |
| 12 | +import qwikCityPlan from '@qwik-city-plan'; |
| 13 | +import { manifest } from '@qwik-client-manifest'; |
| 14 | +import render from './entry.ssr'; |
| 15 | +import express from 'express'; |
| 16 | +import { fileURLToPath } from 'node:url'; |
| 17 | +import { join } from 'node:path'; |
| 18 | + |
| 19 | +declare global { |
| 20 | + type QwikCityPlatform = PlatformNode; |
| 21 | +} |
| 22 | + |
| 23 | +// Directories where the static assets are located |
| 24 | +const distDir = join(fileURLToPath(import.meta.url), '..', '..', 'dist'); |
| 25 | +const buildDir = join(distDir, 'build'); |
| 26 | + |
| 27 | +// Allow for dynamic port |
| 28 | +const PORT = process.env.PORT ?? 3000; |
| 29 | + |
| 30 | +// Create the Qwik City Node middleware |
| 31 | +const { router, notFound } = createQwikCity({ |
| 32 | + render, |
| 33 | + qwikCityPlan, |
| 34 | + manifest, |
| 35 | + // getOrigin(req) { |
| 36 | + // // If deploying under a proxy, you may need to build the origin from the request headers |
| 37 | + // // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Proto |
| 38 | + // const protocol = req.headers["x-forwarded-proto"] ?? "http"; |
| 39 | + // // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Host |
| 40 | + // const host = req.headers["x-forwarded-host"] ?? req.headers.host; |
| 41 | + // return `${protocol}://${host}`; |
| 42 | + // } |
| 43 | +}); |
| 44 | + |
| 45 | +// Create the express server |
| 46 | +// https://expressjs.com/ |
| 47 | +const app = express(); |
| 48 | + |
| 49 | +// Enable gzip compression |
| 50 | +// app.use(compression()); |
| 51 | + |
| 52 | +// Static asset handlers |
| 53 | +// https://expressjs.com/en/starter/static-files.html |
| 54 | +app.use(`/build`, express.static(buildDir, { immutable: true, maxAge: '1y' })); |
| 55 | +app.use(express.static(distDir, { redirect: false })); |
| 56 | + |
| 57 | +// Use Qwik City's page and endpoint request handler |
| 58 | +app.use(router); |
| 59 | + |
| 60 | +// Use Qwik City's 404 handler |
| 61 | +app.use(notFound); |
| 62 | + |
| 63 | +// Start the express server |
| 64 | +app.listen(PORT, () => { |
| 65 | + /* eslint-disable */ |
| 66 | + console.log(`Server started: http://localhost:${PORT}/`); |
| 67 | +}); |
0 commit comments