factory.d.ts 2.9 KB

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