diff --git a/app.js b/app.js index d749732..c321a56 100644 --- a/app.js +++ b/app.js @@ -1,12 +1,18 @@ // Entry point. On Vercel this file is the function: it exports the Express app // and Vercel handles requests. Anywhere else (`npm run dev`, `npm start`) it // listens on PORT as a normal server. +// +// Vercel only recognises an Express project if this file itself imports and +// creates Express, which is why the app is made here rather than in src/. +import express from 'express'; import { ConfigError } from './src/errors.js'; import { createSite, serve } from './src/site.js'; +const app = express(); + let site; try { - site = await createSite(); + site = await createSite({ app }); } catch (error) { // A readable message instead of a stack trace; on Vercel, rethrow so the deploy's logs show it. if (!(error instanceof ConfigError) || process.env.VERCEL) throw error; @@ -16,4 +22,4 @@ try { if (!site.config.onVercel) serve(site); -export default site.app; +export default app; diff --git a/src/http/app.js b/src/http/app.js index a0cfc10..5478e86 100644 --- a/src/http/app.js +++ b/src/http/app.js @@ -1,6 +1,7 @@ import { randomBytes, randomUUID } from 'node:crypto'; import { fileURLToPath } from 'node:url'; import compression from 'compression'; +import ejs from 'ejs'; import express from 'express'; import helmet from 'helmet'; import { pinoHttp } from 'pino-http'; @@ -48,10 +49,17 @@ function sendVersioned(req, res, etag, body) { return res.json(body); } -export function createApp({ config, catalogue, reporter, logger, assets }) { - const app = express(); +/** + * Adds the site's middleware and routes to an Express app. The app is passed in + * (rather than created here) because Vercel's framework detection only + * recognises an entry file that itself imports and creates Express. + */ +export function createApp({ config, catalogue, reporter, logger, assets, app = express() }) { app.disable('x-powered-by'); app.set('trust proxy', config.trustProxy); + // Registered explicitly: Express would load EJS by name at runtime, which Vercel's + // file tracer can't see, so EJS would be missing from the deployed function. + app.engine('ejs', ejs.__express); app.set('view engine', 'ejs'); app.set('views', viewsDir); diff --git a/src/site.js b/src/site.js index b480845..decb948 100644 --- a/src/site.js +++ b/src/site.js @@ -1,4 +1,5 @@ import { fileURLToPath } from 'node:url'; +import express from 'express'; import { waitUntil } from '@vercel/functions'; import { createCatalogue } from './catalogue.js'; import { loadEnvFile, readConfig } from './config.js'; @@ -15,11 +16,11 @@ const SHUTDOWN_TIMEOUT_MS = 10_000; /** * Builds the whole site: configuration, logging, error reporting, TIDAL and - * the Express app. Starts loading playlists, but does not listen on a port, + * the Express app (created by the caller, see app.js). Starts loading playlists, but does not listen on a port, * so the same app runs as a Vercel Function or as a normal server. * Throws a ConfigError when settings are missing or invalid. */ -export async function createSite() { +export async function createSite({ app = express() } = {}) { loadEnvFile(); const config = readConfig(); @@ -58,7 +59,7 @@ export async function createSite() { versioned: config.isProduction, }); - const app = createApp({ config, catalogue, reporter, logger, assets }); + createApp({ app, config, catalogue, reporter, logger, assets }); await catalogue.start(); return { app, config, logger, reporter, assets }; }