Qwik City - Express Middleware

Qwik City Express middleware allows you to connect Qwik City to an Express server for NodeJS.

Below is an example of using the built-in middleware within a user's app. Note that the user can decide the order of the middleware, including handling static assets, and how to handle 404 responses.

import { qwikCity } from '@builder.io/qwik-city/middleware/express';
import express from 'express';
import { fileURLToPath } from 'url';
import { join } from 'path';
import render from './entry.ssr';

// directories where the static assets are located
const distDir = join(fileURLToPath(import.meta.url), '..', '..', 'dist');
const buildDir = join(distDir, 'build');

// create the Qwik City express middleware
const { router, notFound } = qwikCity(render);

// create the express server
const app = express();

// use Qwik City's page and endpoint router
app.use(router);

// static asset handlers
app.use(`/build`, express.static(buildDir, { immutable: true, maxAge: '1y', index: false }));
app.use(express.static(distDir, { index: false }));

// use Qwik City's 404 handler
app.use(notFound);

// start the express server
app.listen(3000);
Made with ♡ by the Builder.io team