factory.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { HttpProxyMiddleware } from './http-proxy-middleware.js';
  2. /**
  3. * Create proxy middleware for Express-like servers. ([list of servers with examples](https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/servers.md))
  4. *
  5. * @example Basic proxy to a single target.
  6. * ```ts
  7. * import { createProxyMiddleware } from 'http-proxy-middleware';
  8. *
  9. * const proxy = createProxyMiddleware({
  10. * target: 'http://www.example.org',
  11. * changeOrigin: true,
  12. * });
  13. * ```
  14. *
  15. * @example Proxy only matching paths and rewrite the forwarded path.
  16. * ```ts
  17. * import { createProxyMiddleware } from 'http-proxy-middleware';
  18. *
  19. * const proxy = createProxyMiddleware({
  20. * target: 'http://localhost:3000',
  21. * pathFilter: '/api',
  22. * pathRewrite: {
  23. * '^/api/': '/',
  24. * },
  25. * });
  26. * ```
  27. *
  28. * @example Native path rewrite by mounting at a route (alternative to `pathRewrite`).
  29. * ```ts
  30. * import express from 'express';
  31. * import { createProxyMiddleware } from 'http-proxy-middleware';
  32. *
  33. * const app = express();
  34. * app.use(
  35. * '/users',
  36. * createProxyMiddleware({
  37. * target: 'http://jsonplaceholder.typicode.com/users',
  38. * changeOrigin: true,
  39. * }),
  40. * );
  41. * ```
  42. *
  43. * @example Use framework-specific request/response types (Express).
  44. * ```ts
  45. * import type { Request, Response } from 'express';
  46. * import { createProxyMiddleware } from 'http-proxy-middleware';
  47. *
  48. * const proxy = createProxyMiddleware<Request, Response>({
  49. * target: 'http://www.example.org/api',
  50. * changeOrigin: true,
  51. * });
  52. * ```
  53. *
  54. * @example Intercept and modify a proxied response body.
  55. * ```ts
  56. * import { createProxyMiddleware, responseInterceptor } from 'http-proxy-middleware';
  57. *
  58. * const proxy = createProxyMiddleware({
  59. * target: 'http://www.example.org',
  60. * selfHandleResponse: true,
  61. * on: {
  62. * proxyRes: responseInterceptor(async (responseBuffer) => {
  63. * const response = responseBuffer.toString('utf8');
  64. * return response.replace('Hello', 'Goodbye');
  65. * }),
  66. * },
  67. * });
  68. * ```
  69. *
  70. * @see https://github.com/chimurai/http-proxy-middleware/
  71. * @see https://github.com/chimurai/http-proxy-middleware/#basic-usage
  72. * @see https://github.com/chimurai/http-proxy-middleware/#intercept-and-manipulate-responses
  73. * @see https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/servers.md
  74. * @see https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/pathFilter.md
  75. * @see https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/pathRewrite.md
  76. * @see https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/response-interceptor.md
  77. */
  78. export function createProxyMiddleware(options) {
  79. const { middleware } = new HttpProxyMiddleware(options);
  80. return middleware;
  81. }