{"id":13527551,"url":"https://github.com/stegano/next-http-proxy-middleware","last_synced_at":"2025-05-15T02:06:13.372Z","repository":{"id":37598739,"uuid":"242898739","full_name":"stegano/next-http-proxy-middleware","owner":"stegano","description":"HTTP Proxy middleware available in API Middleware provided by Next.js.","archived":false,"fork":false,"pushed_at":"2025-04-03T11:56:03.000Z","size":746,"stargazers_count":253,"open_issues_count":15,"forks_count":20,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-14T00:57:31.735Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stegano.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-02-25T03:16:42.000Z","updated_at":"2025-04-13T13:32:44.000Z","dependencies_parsed_at":"2023-02-09T21:15:46.005Z","dependency_job_id":"8b195e0d-64f1-442a-9fb5-92c2cb8f67b0","html_url":"https://github.com/stegano/next-http-proxy-middleware","commit_stats":{"total_commits":124,"total_committers":10,"mean_commits":12.4,"dds":0.6209677419354839,"last_synced_commit":"26858c3889c6035b57632dc7ce485d29d1e9d1b1"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stegano%2Fnext-http-proxy-middleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stegano%2Fnext-http-proxy-middleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stegano%2Fnext-http-proxy-middleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stegano%2Fnext-http-proxy-middleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stegano","download_url":"https://codeload.github.com/stegano/next-http-proxy-middleware/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254259370,"owners_count":22040819,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T06:01:51.068Z","updated_at":"2025-05-15T02:06:13.352Z","avatar_url":"https://github.com/stegano.png","language":"TypeScript","readme":"# Next.js HTTP Proxy Middleware\n![NPM License](https://img.shields.io/npm/l/next-http-proxy-middleware)\n![NPM Downloads](https://img.shields.io/npm/dw/next-http-proxy-middleware) \u003c!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --\u003e\n[![All Contributors](https://img.shields.io/badge/all_contributors-14-orange.svg?style=flat-square)](#contributors-)\n\u003c!-- ALL-CONTRIBUTORS-BADGE:END --\u003e\n\nHTTP Proxy middleware available in API Middleware provided by Next.js.\n\n\n## ⭐️ Before using\nPlease try the solutions below before using this library. 😀\n\n### Try using `Next.js` Rewrites(recommended)\n* This function is supported by Next.js. No additional libraries need to be installed!\n   * https://nextjs.org/docs/api-reference/next.config.js/rewrites\n   ```ts\n   // next.config.js\n  async rewrites() {\n    return [\n      {\n        source: \"/api/:path*\",\n        destination: \"http://example.com/api/:path*\",\n      },\n    ];\n  },\n   ```\n\n### Try using `Http-Proxy`\n* `next-http-proxy-middleware` is implemented using `http-proxy` internally. Since the implementation is not complicated, it is recommended to try `http-proxy` directly.\n  ```ts\n  // pages/api/[...all].ts\n  import httpProxy from \"http-proxy\";\n  \n  export const config = {\n    api: {\n      // Enable `externalResolver` option in Next.js\n      externalResolver: true,\n      bodyParser: false,\n    },\n  };\n\n  export default (req, res) =\u003e\n    new Promise((resolve, reject) =\u003e {\n      const proxy: httpProxy = httpProxy.createProxy();\n      proxy.once(\"proxyRes\", resolve).once(\"error\", reject).web(req, res, {\n        changeOrigin: true,\n        target: process.env.NEXT_PUBLIC_API_PROXY_URL,\n      });\n    });\n  ```\n\n## Installation\n\nThe easiest way to install `next-http-proxy-middleware` is with [npm](https://www.npmjs.com/).\n\n```bash\nnpm install next-http-proxy-middleware\n```\n\nAlternately, download the source.\n\n```bash\ngit clone https://github.com/stegano/next-http-proxy-middleware.git\n```\n\n## Features\n\nThis middleware is implemented using the [`http-proxy`](https://www.npmjs.com/package/http-proxy) library. You can use the existing options provided by `http-proxy`. And you can rewrite the api path using `pathRewrite`, an additional option provided by this middleware.\n\n- [http-proxy options](https://www.npmjs.com/package/http-proxy#options)\n\n### `pathRewrite` option\n\n- Replaces URL information matching the pattern with another string.\n  - Priority is determined in the order entered in the array.\n  - If the request URL matches the pattern `pathRewrite.patternStr` replace the URL string with the value `pathRewrite.replaceStr`.\n\n### `onProxyInit` option\n- You can access the `http-proxy` instance using the `onProxyInit` option. See the example below.\n  \n  ```ts\n  import type { NextApiRequest, NextApiResponse } from \"next\";\n  import type { NextHttpProxyMiddlewareOptions } from \"next-http-proxy-middleware\";\n  import httpProxyMiddleware from \"next-http-proxy-middleware\";\n\n  const handleProxyInit: NextHttpProxyMiddlewareOptions[\"onProxyInit\"] = (proxy) =\u003e {\n    /**\n    * Check the list of bindable events in the `http-proxy` specification.\n    * @see https://www.npmjs.com/package/http-proxy#listening-for-proxy-events\n    */\n    proxy.on(\"proxyReq\", (proxyReq, req, res) =\u003e {\n      // ...\n    });\n    proxy.on(\"proxyRes\", (proxyRes, req, res) =\u003e {\n      // ...\n    });\n  };\n\n  export default async (req: NextApiRequest, res: NextApiResponse) =\u003e\n    httpProxyMiddleware(req, res, {\n      target: \"http://example.com\",\n      onProxyInit: handleProxyInit,\n    });\n  ```\n\n#### Example\n\n- Refer to the following for how to use Next.js API Middleware\n\n  - [Next.js API Middlewares Guide](https://nextjs.org/docs/api-routes/api-middlewares)\n\n  ```ts\n  // pages/api/[...all].ts\n  import type { NextApiRequest, NextApiResponse } from \"next\";\n  import httpProxyMiddleware from \"next-http-proxy-middleware\";\n\n  const isDevelopment = process.env.NODE_ENV !== \"production\";\n\n  export const config = {\n    api: {\n      // Enable `externalResolver` option in Next.js\n      externalResolver: true,\n    },\n  }\n\n  export default (req: NextApiRequest, res: NextApiResponse) =\u003e (\n    isDevelopment\n      ? httpProxyMiddleware(req, res, {\n        // You can use the `http-proxy` option\n        target: \"https://www.example.com\",\n        // In addition, you can use the `pathRewrite` option provided by `next-http-proxy-middleware`\n        pathRewrite: [{\n          patternStr: \"^/api/new\",\n          replaceStr: \"/v2\"\n        }, {\n          patternStr: \"^/api\",\n          replaceStr: \"\"\n        }],\n      })\n      : res.status(404).send(null)\n  );\n  ```\n  - `externalResolver` is an explicit flag that tells the server that this route is being handled by an external resolver. Enabling this option disables warnings for unresolved requests.\n    - See the issues below\n      - https://github.com/stegano/next-http-proxy-middleware/issues/32\n      - https://github.com/stegano/next-http-proxy-middleware/issues/21\n\n#### Using `multipart/form-data`\n* If you are using the `multipart/form-data`, refer to the Issues below\n  * https://github.com/stegano/next-http-proxy-middleware/issues/33\n  * https://github.com/vercel/next.js/pull/7686\n\n\n## Other projects\n* [ReactRenderStateHook](https://www.npmjs.com/package/react-render-state-hook)\n  * `react-render-state-hook` is a React hook that enables declarative management of components based on data processing states. It facilitates straightforward state management and data sharing among multiple components in a React.\n\n\n## Contributors ✨\n\nThanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --\u003e\n\u003c!-- prettier-ignore-start --\u003e\n\u003c!-- markdownlint-disable --\u003e\n\u003ctable\u003e\n  \u003ctbody\u003e\n    \u003ctr\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"http://iamdenny.com\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/1505166?v=4?s=100\" width=\"100px;\" alt=\"Denny Lim\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eDenny Lim\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/stegano/next-http-proxy-middleware/issues?q=author%3Aiamdenny\" title=\"Bug reports\"\u003e🐛\u003c/a\u003e \u003ca href=\"https://github.com/stegano/next-http-proxy-middleware/commits?author=iamdenny\" title=\"Code\"\u003e💻\u003c/a\u003e\u003c/td\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"https://github.com/larrifax\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/144189?v=4?s=100\" width=\"100px;\" alt=\"Kristian Tryggestad\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eKristian Tryggestad\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/stegano/next-http-proxy-middleware/issues?q=author%3Alarrifax\" title=\"Bug reports\"\u003e🐛\u003c/a\u003e \u003ca href=\"https://github.com/stegano/next-http-proxy-middleware/commits?author=larrifax\" title=\"Code\"\u003e💻\u003c/a\u003e\u003c/td\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"https://github.com/gthb\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/153580?v=4?s=100\" width=\"100px;\" alt=\"Gunnlaugur Thor Briem\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eGunnlaugur Thor Briem\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/stegano/next-http-proxy-middleware/commits?author=gthb\" title=\"Code\"\u003e💻\u003c/a\u003e \u003ca href=\"#ideas-gthb\" title=\"Ideas, Planning, \u0026 Feedback\"\u003e🤔\u003c/a\u003e\u003c/td\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"https://ottovw.com\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/1045946?v=4?s=100\" width=\"100px;\" alt=\"Otto von Wesendonk\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eOtto von Wesendonk\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"#security-ottovw\" title=\"Security\"\u003e🛡️\u003c/a\u003e\u003c/td\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"https://github.com/dsilvasc\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/24484414?v=4?s=100\" width=\"100px;\" alt=\"Daniel Silva\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eDaniel Silva\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"#ideas-dsilvasc\" title=\"Ideas, Planning, \u0026 Feedback\"\u003e🤔\u003c/a\u003e\u003c/td\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"https://lumenstudio.dev/\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/5436545?v=4?s=100\" width=\"100px;\" alt=\"Yann Pringault\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eYann Pringault\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/stegano/next-http-proxy-middleware/commits?author=Kerumen\" title=\"Code\"\u003e💻\u003c/a\u003e\u003c/td\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"https://github.com/lorenzodejong\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/30781484?v=4?s=100\" width=\"100px;\" alt=\"Lorenzo\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eLorenzo\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/stegano/next-http-proxy-middleware/commits?author=lorenzodejong\" title=\"Documentation\"\u003e📖\u003c/a\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"https://medium.com/@timon.grassl\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/34568407?v=4?s=100\" width=\"100px;\" alt=\"Timon Grassl\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eTimon Grassl\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/stegano/next-http-proxy-middleware/issues?q=author%3Atgrassl\" title=\"Bug reports\"\u003e🐛\u003c/a\u003e\u003c/td\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"https://github.com/abhinavkumar940\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/1189133?v=4?s=100\" width=\"100px;\" alt=\"Abhinav Kumar\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eAbhinav Kumar\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/stegano/next-http-proxy-middleware/commits?author=abhinavkumar940\" title=\"Documentation\"\u003e📖\u003c/a\u003e\u003c/td\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"https://jackcuthbert.dev/\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/5564612?v=4?s=100\" width=\"100px;\" alt=\"Jack Cuthbert\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eJack Cuthbert\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/stegano/next-http-proxy-middleware/commits?author=JackCuthbert\" title=\"Documentation\"\u003e📖\u003c/a\u003e\u003c/td\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"https://vytenis.kuciauskas.lt\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/468006?v=4?s=100\" width=\"100px;\" alt=\"Vytenis\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eVytenis\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/stegano/next-http-proxy-middleware/commits?author=FDiskas\" title=\"Documentation\"\u003e📖\u003c/a\u003e\u003c/td\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"https://dariosky.it\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/705644?v=4?s=100\" width=\"100px;\" alt=\"Dario Varotto\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eDario Varotto\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/stegano/next-http-proxy-middleware/commits?author=dariosky\" title=\"Documentation\"\u003e📖\u003c/a\u003e\u003c/td\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"https://github.com/johannbrynjar\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/2641440?v=4?s=100\" width=\"100px;\" alt=\"johannbrynjar\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003ejohannbrynjar\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/stegano/next-http-proxy-middleware/issues?q=author%3Ajohannbrynjar\" title=\"Bug reports\"\u003e🐛\u003c/a\u003e \u003ca href=\"https://github.com/stegano/next-http-proxy-middleware/commits?author=johannbrynjar\" title=\"Code\"\u003e💻\u003c/a\u003e\u003c/td\u003e\n      \u003ctd align=\"center\" valign=\"top\" width=\"14.28%\"\u003e\u003ca href=\"https://github.com/bever1337\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/28774413?v=4?s=100\" width=\"100px;\" alt=\"bever1337\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003ebever1337\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/stegano/next-http-proxy-middleware/commits?author=bever1337\" title=\"Documentation\"\u003e📖\u003c/a\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n  \u003c/tbody\u003e\n\u003c/table\u003e\n\n\u003c!-- markdownlint-restore --\u003e\n\u003c!-- prettier-ignore-end --\u003e\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:END --\u003e\n\nThis project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!\n","funding_links":[],"categories":["TypeScript","🤖 AI \u0026 Machine Learning"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstegano%2Fnext-http-proxy-middleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstegano%2Fnext-http-proxy-middleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstegano%2Fnext-http-proxy-middleware/lists"}