diff --git a/snowpack/src/commands/dev.ts b/snowpack/src/commands/dev.ts index b5b2092af3..6be89a9f3b 100644 --- a/snowpack/src/commands/dev.ts +++ b/snowpack/src/commands/dev.ts @@ -835,7 +835,15 @@ ${err}`); const server = createServer(async (req, res) => { try { - return await requestHandler(req, res); + // Allow users to supply Express-style middleware (or a full Express app) to handle requests + // before Snowpack receives them. Snowpack will handle anything the middleware doesn't. + const middleware = config.devOptions.middleware; + if (typeof middleware === 'function') { + const next = () => requestHandler(req, res); + middleware(req, res, next); + } else { + await requestHandler(req, res); + } } catch (err) { logger.error(`[500] ${req.url}`); logger.error(err.toString() || err); diff --git a/snowpack/src/types/snowpack.ts b/snowpack/src/types/snowpack.ts index 192649a4fc..36c4960c7f 100644 --- a/snowpack/src/types/snowpack.ts +++ b/snowpack/src/types/snowpack.ts @@ -1,4 +1,5 @@ import type HttpProxy from 'http-proxy'; +import type * as http from 'http'; import type {InstallOptions} from 'esinstall'; export type DeepPartial = { @@ -128,6 +129,7 @@ export interface SnowpackConfig { fallback: string; open: string; hmr?: boolean; + middleware?: (req: http.IncomingMessage, res: http.ServerResponse, next: () => void) => unknown; hmrDelay: number; }; installOptions: InstallOptions;