http-proxy-middleware

  • Version 3.0.3
  • Published
  • 81.4 kB
  • 6 dependencies
  • MIT license

Install

npm i http-proxy-middleware
yarn add http-proxy-middleware
pnpm add http-proxy-middleware

Overview

The one-liner node.js proxy middleware for connect, express, next.js and more

Index

Variables

variable debugProxyErrorsPlugin

const debugProxyErrorsPlugin: Plugin<http.IncomingMessage, http.ServerResponse>;
  • Subscribe to to prevent server from crashing. Errors are logged with library.

variable errorResponsePlugin

const errorResponsePlugin: Plugin<http.IncomingMessage, http.ServerResponse>;

    variable loggerPlugin

    const loggerPlugin: Plugin<http.IncomingMessage, http.ServerResponse>;

      variable proxyEventsPlugin

      const proxyEventsPlugin: Plugin<http.IncomingMessage, http.ServerResponse>;
      • Implements option.on object to subscribe to http-proxy events.

        Example 1

        createProxyMiddleware({
        on: {
        error: (error, req, res, target) => {},
        proxyReq: (proxyReq, req, res, options) => {},
        proxyReqWs: (proxyReq, req, socket, options) => {},
        proxyRes: (proxyRes, req, res) => {},
        open: (proxySocket) => {},
        close: (proxyRes, proxySocket, proxyHead) => {},
        start: (req, res, target) => {},
        end: (req, res, proxyRes) => {},
        econnreset: (error, req, res, target) => {},
        }
        });

      Functions

      function createProxyMiddleware

      createProxyMiddleware: <
      TReq = http.IncomingMessage,
      TRes = http.ServerResponse,
      TNext = (err?: any) => void
      >(
      options: Options<TReq, TRes>
      ) => RequestHandler<TReq, TRes, TNext>;

        function fixRequestBody

        fixRequestBody: <TReq = http.IncomingMessage>(
        proxyReq: http.ClientRequest,
        req: TReq
        ) => void;
        • Fix proxied body if bodyParser is involved.

        function legacyCreateProxyMiddleware

        legacyCreateProxyMiddleware: {
        <TReq = http.IncomingMessage, TRes = http.ServerResponse>(
        shortHand: string
        ): RequestHandler<TReq, TRes>;
        <TReq = http.IncomingMessage, TRes = http.ServerResponse>(
        legacyOptions: LegacyOptions<TReq, TRes>
        ): RequestHandler<TReq, TRes, (err?: any) => void>;
        <TReq = http.IncomingMessage, TRes = http.ServerResponse>(
        legacyContext: Filter<TReq>,
        legacyOptions: LegacyOptions<TReq, TRes>
        ): RequestHandler<TReq, TRes, (err?: any) => void>;
        };
        • Deprecated

          This function is deprecated and will be removed in a future version.

          Use createProxyMiddleware instead.

        function responseInterceptor

        responseInterceptor: <
        TReq extends http.IncomingMessage = http.IncomingMessage,
        TRes extends http.ServerResponse = http.ServerResponse
        >(
        interceptor: Interceptor<TReq, TRes>
        ) => (proxyRes: TReq, req: TReq, res: TRes) => Promise<void>;
        • Intercept responses from upstream. Automatically decompress (deflate, gzip, brotli). Give developer the opportunity to modify intercepted Buffer and http.ServerResponse

          NOTE: must set options.selfHandleResponse=true (prevent automatic call of res.end())

        Interfaces

        interface LegacyOptions

        interface LegacyOptions<TReq = http.IncomingMessage, TRes = http.ServerResponse>
        extends Options<TReq, TRes> {}
        • Deprecated

          Will be removed in a future version.

        property logLevel

        logLevel?: any;
        • Example 1

          {
          logger: console
          }

          Deprecated

          Use logger instead.

        property logProvider

        logProvider?: any;
        • Example 1

          {
          logger: console
          }

          Deprecated

          Use logger instead.

        property onClose

        onClose?: (...args: any[]) => void;
        • Example 1

          {
          on: {
          close: () => {}
          }

          Deprecated

          Use on.close instead.

        property onError

        onError?: (...args: any[]) => void;
        • Example 1

          {
          on: {
          error: () => {}
          }

          Deprecated

          Use on.error instead.

        property onOpen

        onOpen?: (...args: any[]) => void;
        • Example 1

          {
          on: {
          open: () => {}
          }

          Deprecated

          Use on.open instead.

        property onProxyReq

        onProxyReq?: (...args: any[]) => void;
        • Example 1

          {
          on: {
          proxyReq: () => {}
          }

          Deprecated

          Use on.proxyReq instead.

        property onProxyReqWs

        onProxyReqWs?: (...args: any[]) => void;
        • Example 1

          {
          on: {
          proxyReqWs: () => {}
          }

          Deprecated

          Use on.proxyReqWs instead.

        property onProxyRes

        onProxyRes?: (...args: any[]) => void;
        • Example 1

          {
          on: {
          proxyRes: () => {}
          }

          Deprecated

          Use on.proxyRes instead.

        interface Options

        interface Options<TReq = http.IncomingMessage, TRes = http.ServerResponse>
        extends httpProxy.ServerOptions {}

          property ejectPlugins

          ejectPlugins?: boolean;
          • Eject pre-configured plugins. NOTE: register your own error handlers to prevent server from crashing.

            https://github.com/chimurai/http-proxy-middleware#ejectplugins-boolean-default-false v3.0.0

          property logger

          logger?: Logger | any;
          • Log information from http-proxy-middleware

            Example 1

            createProxyMiddleware({
            logger: console
            });

            https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/logger.md v3.0.0

          property on

          on?: OnProxyEvent<TReq, TRes>;
          • Listen to http-proxy events

            Example 1

            createProxyMiddleware({
            on: {
            error: (error, req, res, target) => {
            console.error(error);
            }
            }
            });

            https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/proxy-events.md v3.0.0

            See Also

          property pathFilter

          pathFilter?: Filter<TReq>;
          • Narrow down requests to proxy or not. Filter on which is relative to the proxy's "mounting" point in the server. Or use the object for more complex filtering. https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/pathFilter.md v3.0.0

          property pathRewrite

          pathRewrite?:
          | {
          [regexp: string]: string;
          }
          | ((path: string, req: TReq) => string | undefined)
          | ((path: string, req: TReq) => Promise<string>);
          • Modify request paths before requests are send to the target.

            Example 1

            createProxyMiddleware({
            pathRewrite: {
            '^/api/old-path': '/api/new-path', // rewrite path
            }
            });

            https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/pathRewrite.md

          property plugins

          plugins?: Plugin<TReq, TRes>[];
          • Access the internal http-proxy server instance to customize behavior

            Example 1

            createProxyMiddleware({
            plugins: [(proxyServer, options) => {
            proxyServer.on('error', (error, req, res) => {
            console.error(error);
            });
            }]
            });

            https://github.com/chimurai/http-proxy-middleware#plugins-array v3.0.0

          property router

          router?:
          | {
          [hostOrPath: string]: httpProxy.ServerOptions['target'];
          }
          | ((req: TReq) => httpProxy.ServerOptions['target'])
          | ((req: TReq) => Promise<httpProxy.ServerOptions['target']>);
          • Dynamically set the .

            Example 1

            createProxyMiddleware({
            router: async (req) => {
            return 'http://127:0.0.1:3000';
            }
            });

            https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/router.md

          interface RequestHandler

          interface RequestHandler<
          TReq = http.IncomingMessage,
          TRes = http.ServerResponse,
          TNext = NextFunction
          > {}

            property upgrade

            upgrade: (req: http.IncomingMessage, socket: net.Socket, head: Buffer) => void;

              call signature

              (req: TReq, res: TRes, next?: TNext): Promise<void>;

                Type Aliases

                type Filter

                type Filter<TReq = http.IncomingMessage> =
                | string
                | string[]
                | ((pathname: string, req: TReq) => boolean);

                  Package Files (11)

                  Dependencies (6)

                  Dev Dependencies (31)

                  Peer Dependencies (0)

                  No peer dependencies.

                  Badge

                  To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                  You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/http-proxy-middleware.

                  • Markdown
                    [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/http-proxy-middleware)
                  • HTML
                    <a href="https://www.jsdocs.io/package/http-proxy-middleware"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>