Skip to content

Latest commit

 

History

History
163 lines (118 loc) · 3.72 KB

MIGRATION.md

File metadata and controls

163 lines (118 loc) · 3.72 KB

Migration guide

v2 to v3 adapter

legacyCreateProxyMiddleware

Use the adapter to use v3 with minimal changes to your v2 implementation.

NOTE: legacyCreateProxyMiddleware will be removed in a future version.

// before
const { createProxyMiddleware } = require('http-proxy-middleware');

createProxyMiddleware(...);

// after
const { legacyCreateProxyMiddleware } = require('http-proxy-middleware');

legacyCreateProxyMiddleware(...);
// before
import { createProxyMiddleware, Options } from 'http-proxy-middleware';

createProxyMiddleware(...);

// after
import { legacyCreateProxyMiddleware, LegacyOptions } from 'http-proxy-middleware';

legacyCreateProxyMiddleware(...);

v3 breaking changes

Removed req.url patching

// before
app.use('/user', proxy({ target: 'http://www.example.org' }));

// after
app.use('/user', proxy({ target: 'http://www.example.org/user' }));

pathRewrite (potential behavior change)

Related to removal of req.url patching.

pathRewrite now only rewrites the path after the mount point.

It was common to rewrite the basePath with the pathRewrite option:

// before
app.use('/user', proxy({
  target: 'http://www.example.org'
  pathRewrite: { '^/user': '/secret' }
}));

// after
app.use('/user', proxy({ target: 'http://www.example.org/secret' }));

When proxy is mounted at the root, pathRewrite should still work as in v2.

// not affected
app.use(proxy({
  target: 'http://www.example.org'
  pathRewrite: { '^/user': '/secret' }
}));

Removed "shorthand" usage

Specify the target option.

// before
createProxyMiddleware('http:/www.example.org');

// after
createProxyMiddleware({ target: 'http:/www.example.org' });

Removed context argument

The context argument has been moved to option: pathFilter.

Functionality did not change.

See recipes/pathFilter.md for more information.

// before
createProxyMiddleware('/path', { target: 'http://www.example.org' });

// after
createProxyMiddleware({
  target: 'http://www.example.org',
  pathFilter: '/path',
});

Removed logProvider and logLevel options

Use your external logging library to log and control the logging level.

Only info, warn, error are used internally for compatibility across different loggers.

If you use winston, make sure to enable interpolation: https://github.com/winstonjs/winston#string-interpolation

See recipes/logger.md for more information.

// new
createProxyMiddleware({
  target: 'http://www.example.org',
  logger: console,
});

Refactored proxy events

See recipes/proxy-events.md for more information.

// before
createProxyMiddleware({
  target: 'http://www.example.org',
  onError: () => {},
  onProxyReq: () => {},
  onProxyRes: () => {},
  onProxyReqWs: () => {},
  onOpen: () => {},
  onClose: () => {},
});

// after
createProxyMiddleware({
  target: 'http://www.example.org',
  on: {
    error: () => {},
    proxyReq: () => {},
    proxyRes: () => {},
    proxyReqWs: () => {},
    open: () => {},
    close: () => {},
  },
});