How to use the createProxyMiddleware function from http-proxy-middleware

Find comprehensive JavaScript http-proxy-middleware.createProxyMiddleware code examples handpicked from public code repositorys.

http-proxy-middleware.createProxyMiddleware is a function that creates a middleware to proxy HTTP requests to another server.

27
28
29
30
31
32
33
34
35
36
37
// Start Express App
const app = express()


// Configure and add ClickUp API proxy
// Ref: https://github.com/chimurai/http-proxy-middleware/blob/master/examples/express/index.js
const jsonPlaceholderProxy = createProxyMiddleware({
  target: 'https://api.clickup.com',
  changeOrigin: true,
  secure: false,
  logLevel: 'warn',
fork icon2
star icon1
watch icon6

+ 4 other calls in file

1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
// It is possible to use the `bypass` method without a `target` or `router`.
// However, the proxy middleware has no use in this case, and will fail to instantiate.
if (proxyConfig.target) {
  const context = proxyConfig.context || proxyConfig.path;

  return createProxyMiddleware(context, proxyConfig);
}

if (proxyConfig.router) {
  return createProxyMiddleware(proxyConfig);
fork icon0
star icon0
watch icon1

+ 9 other calls in file

How does http-proxy-middleware.createProxyMiddleware work?

http-proxy-middleware.createProxyMiddleware works by creating a middleware function that intercepts incoming HTTP requests and forwards them to a target server. The middleware can be configured with various options to control the behavior of the proxy, such as the target URL, headers to add to the request, and whether to rewrite the request or response. When a request comes in, the middleware checks if it matches any of the configured routes. If a match is found, the middleware sets up a proxy request to the target server and forwards the request headers and body. The middleware then waits for the response from the target server and forwards it back to the client, along with any modifications made to the response. One of the key features of http-proxy-middleware is that it supports WebSocket connections as well as standard HTTP requests. When a WebSocket connection is detected, the middleware upgrades the connection to a WebSocket protocol and sets up a bidirectional data stream between the client and server. Overall, http-proxy-middleware.createProxyMiddleware provides a powerful and flexible way to proxy HTTP requests to another server, with support for many advanced features such as WebSocket connections and request/response rewriting.

2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
// It is possible to use the `bypass` method without a `target` or `router`.
// However, the proxy middleware has no use in this case, and will fail to instantiate.
if (proxyConfig.target) {
  const context = proxyConfig.context || proxyConfig.path;

  return createProxyMiddleware(
    /** @type {string} */ (context),
    proxyConfig
  );
}
fork icon0
star icon0
watch icon1

+ 9 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
const { createProxyMiddleware } = require("http-proxy-middleware");

const apiProxy = createProxyMiddleware("/api", {
  target: "https://example.com",
  changeOrigin: true,
});

module.exports = function (app) {
  app.use(apiProxy);
};

In this example, we're creating a proxy middleware using http-proxy-middleware.createProxyMiddleware that will match any requests to the /api path and forward them to the remote server at https://example.com. We're setting the changeOrigin option to true to modify the Origin header of the request to match the target URL, which can be useful for handling CORS errors. We're then exporting a function that sets up the middleware using the Express.js app.use() method. This ensures that any requests to the local development server that match the /api path will be forwarded to the remote server. This demonstrates how http-proxy-middleware.createProxyMiddleware can be used to create a flexible and configurable proxy middleware that can be integrated with an Express.js server.