Skip to content

Commit

Permalink
Fixes #970 by adding middleware dev config option. (#1062)
Browse files Browse the repository at this point in the history
Co-authored-by: Josh Wilson <[email protected]>
  • Loading branch information
joshwilsonvu and Josh Wilson authored Sep 26, 2020
1 parent e5fdb36 commit eba63e3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 9 additions & 1 deletion snowpack/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,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);
Expand Down
2 changes: 2 additions & 0 deletions snowpack/src/types/snowpack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type HttpProxy from 'http-proxy';
import type * as http from 'http';
import type {InstallOptions} from 'esinstall';

export type DeepPartial<T> = {
Expand Down Expand Up @@ -129,6 +130,7 @@ export interface SnowpackConfig {
fallback: string;
open: string;
hmr?: boolean;
middleware?: (req: http.IncomingMessage, res: http.ServerResponse, next: () => void) => unknown;
hmrDelay: number;
hmrPort: number;
};
Expand Down

1 comment on commit eba63e3

@vercel
Copy link

@vercel vercel bot commented on eba63e3 Sep 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.