Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #970 by adding middleware dev config option. #1062

Merged
merged 2 commits into from
Sep 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion snowpack/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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 @@ -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;
Expand Down