{"id":21524367,"url":"https://github.com/2o3t/koa2-proxy-middleware","last_synced_at":"2026-05-22T05:03:34.256Z","repository":{"id":35044773,"uuid":"182246018","full_name":"2o3t/koa2-proxy-middleware","owner":"2o3t","description":"http proxy middleware for koa2","archived":false,"fork":false,"pushed_at":"2022-12-03T06:45:32.000Z","size":294,"stargazers_count":0,"open_issues_count":6,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-25T05:48:00.863Z","etag":null,"topics":["2o3t","cors","http","http-proxy-middleware","https","koa","koa-proxy-middleware","koa2","koa2-proxy-middleware","middleware","proxy"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/2o3t.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}},"created_at":"2019-04-19T10:12:57.000Z","updated_at":"2020-03-15T18:25:00.000Z","dependencies_parsed_at":"2023-01-15T12:40:09.148Z","dependency_job_id":null,"html_url":"https://github.com/2o3t/koa2-proxy-middleware","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/2o3t%2Fkoa2-proxy-middleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/2o3t%2Fkoa2-proxy-middleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/2o3t%2Fkoa2-proxy-middleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/2o3t%2Fkoa2-proxy-middleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/2o3t","download_url":"https://codeload.github.com/2o3t/koa2-proxy-middleware/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244085031,"owners_count":20395523,"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":["2o3t","cors","http","http-proxy-middleware","https","koa","koa-proxy-middleware","koa2","koa2-proxy-middleware","middleware","proxy"],"created_at":"2024-11-24T01:24:29.898Z","updated_at":"2026-05-22T05:03:34.202Z","avatar_url":"https://github.com/2o3t.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @2o3t/koa2-proxy-middleware\n\nNode.js proxying made simple. Configure proxy middleware with ease for [koa2](https://github.com/koajs/koa).\n\nPowered by [`http-proxy-middleware`](https://github.com/chimurai/http-proxy-middleware). [![GitHub stars](https://img.shields.io/github/stars/chimurai/http-proxy-middleware.svg?style=social\u0026label=Star)](https://github.com/chimurai/http-proxy-middleware)\n\n## TL;DR\n\nProxy `/api` requests to `http://www.2o3t.cn`\n\n```javascript\nconst Koa = require('koa');\nconst app = new Koa();\nconst proxy = require('@2o3t/koa2-proxy-middleware');\n\n// app.use(proxy({ target: 'http://www.2o3t.cn', changeOrigin: true }));\n\nconst Router = require('koa-router');\nconst router = new Router();\nrouter.use(\n  '/api',\n  proxy({ target: 'http://www.2o3t.cn', changeOrigin: true })\n);\napp.use(router.routes())\n  .use(router.allowedMethods());\napp.listen(3000);\n// http://localhost:3000/api/foo/bar -\u003e http://www.2o3t.cn/api/foo/bar\n```\n\n_All_ `http-proxy` [options](https://github.com/nodejitsu/node-http-proxy#options) can be used, along with some extra `http-proxy-middleware` [options](#options).\n\n:bulb: **Tip:** Set the option `changeOrigin` to `true` for [name-based virtual hosted sites](http://en.wikipedia.org/wiki/Virtual_hosting#Name-based).\n\n## Install\n\n```javascript\n$ npm install --save-dev @2o3t/koa2-proxy-middleware\n// or\nyarn add @2o3t/koa2-proxy-middleware\n```\n\n## Core concept\n\nProxy middleware configuration.\n\n#### proxy([context,] config)\n\n```javascript\nconst proxy = require('@2o3t/koa2-proxy-middleware');\n\nconst apiProxy = proxy('/api', { target: 'http://www.2o3t.cn' });\n//                   \\____/   \\_____________________________/\n//                     |                    |\n//                   context             options\n\n// 'apiProxy' is now ready to be used as middleware in a server.\n```\n\n- **context**: Determine which requests should be proxied to the target host.\n  (more on [context matching](#context-matching))\n- **options.target**: target host to proxy to. _(protocol + host)_\n\n(full list of [`http-proxy-middleware` configuration options](#options))\n\n#### proxy(uri [, config])\n\n```javascript\n// shorthand syntax for the example above:\nconst apiProxy = proxy('http://www.2o3t.cn/api');\n```\n\nMore about the [shorthand configuration](#shorthand).\n\n## Example\n\nAn example with `koa2` server.\n\n```javascript\nconst Koa = require('koa');\nconst app = new Koa();\nconst proxy = require('@2o3t/koa2-proxy-middleware');\n\n// proxy middleware options\nconst options = {\n  target: 'http://www.2o3t.cn', // target host\n  changeOrigin: true, // needed for virtual hosted sites\n  ws: true, // proxy websockets\n  pathRewrite: {\n    '^/api/old-path': '/api/new-path', // rewrite path\n    '^/api/remove/path': '/path' // remove base path\n  },\n  router: {\n    // when request.headers.host == 'dev.localhost:3000',\n    // override target 'http://www.2o3t.cn' to 'http://localhost:8000'\n    'dev.localhost:3000': 'http://localhost:8000'\n  }\n};\n\n// create the proxy (without context)\nconst exampleProxy = proxy(options);\n\napp.use(exampleProxy)\n\napp.use(ctx =\u003e {\n    console.log(ctx.status);\n});\n\napp.listen(3003);\n```\n\n## Context matching\n\nProviding an alternative way to decide which requests should be proxied; In case you are not able to use the server's [`path` parameter](http://expressjs.com/en/4x/api.html#app.use) to mount the proxy or when you need more flexibility.\n\n[RFC 3986 `path`](https://tools.ietf.org/html/rfc3986#section-3.3) is used for context matching.\n\n```\n         foo://example.com:8042/over/there?name=ferret#nose\n         \\_/   \\______________/\\_________/ \\_________/ \\__/\n          |           |            |            |        |\n       scheme     authority       path        query   fragment\n```\n\n- **path matching**\n\n  - `proxy({...})` - matches any path, all requests will be proxied.\n  - `proxy('/', {...})` - matches any path, all requests will be proxied.\n  - `proxy('/api', {...})` - matches paths starting with `/api`\n\n- **multiple path matching**\n\n  - `proxy(['/api', '/ajax', '/someotherpath'], {...})`\n\n- **wildcard path matching**\n\n  For fine-grained control you can use wildcard matching. Glob pattern matching is done by _micromatch_. Visit [micromatch](https://www.npmjs.com/package/micromatch) or [glob](https://www.npmjs.com/package/glob) for more globbing examples.\n\n  - `proxy('**', {...})` matches any path, all requests will be proxied.\n  - `proxy('**/*.html', {...})` matches any path which ends with `.html`\n  - `proxy('/*.html', {...})` matches paths directly under path-absolute\n  - `proxy('/api/**/*.html', {...})` matches requests ending with `.html` in the path of `/api`\n  - `proxy(['/api/**', '/ajax/**'], {...})` combine multiple patterns\n  - `proxy(['/api/**', '!**/bad.json'], {...})` exclusion\n\n  **Note**: In multiple path matching, you cannot use string paths and wildcard paths together.\n\n- **custom matching**\n\n  For full control you can provide a custom function to determine which requests should be proxied or not.\n\n  ```javascript\n  /**\n   * @return {Boolean}\n   */\n  const filter = function(pathname, req) {\n    return pathname.match('^/api') \u0026\u0026 req.method === 'GET';\n  };\n\n  const apiProxy = proxy(filter, { target: 'http://www.2o3t.cn' });\n  ```\n\n## Options\n\n### [`http-proxy-middleware` configuration options](https://github.com/chimurai/http-proxy-middleware#options)\n\n### new options\n\n- `option.proxyBody`: [Boolean|Function] Collect the results returned by the agent and assign them to `ctx.body`. Default: false.\n\n```js\noption.proxyBody: true,\n// or\noption.proxyBody: function(json, proxyRes, req, res, ctx) {\n    ctx.body = json;\n}\n```\n\n## License\n\nThe MIT License (MIT)\n\nCopyright (c) 2018-2019 Zyao89\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F2o3t%2Fkoa2-proxy-middleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F2o3t%2Fkoa2-proxy-middleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F2o3t%2Fkoa2-proxy-middleware/lists"}