Modify request paths before requests are send to the target.
Rewrite paths
const { createProxyMiddleware } = require('http-proxy-middleware');
const options = {
target: 'http://localhost:3000',
pathRewrite: {
'^/api/old-path': '/api/new-path', // rewrite path
},
};
const apiProxy = createProxyMiddleware('/api', options);
// `/old/api/foo/bar` -> `http://localhost:3000/new/api/foo/bar`
Remove base path
const { createProxyMiddleware } = require('http-proxy-middleware');
const options = {
target: 'http://localhost:3000',
pathRewrite: {
'^/api/': '/', // remove base path
},
};
const apiProxy = createProxyMiddleware('/api', options);
// `/api/lorum/ipsum` -> `http://localhost:3000/lorum/ipsum`
Add base path
const { createProxyMiddleware } = require('http-proxy-middleware');
const options = {
target: 'http://localhost:3000',
pathRewrite: {
'^/': '/extra/', // add base path
},
};
const apiProxy = createProxyMiddleware('/api', options);
// `/api/lorum/ipsum` -> `http://localhost:3000/extra/api/lorum/ipsum`
Implement you own path rewrite logic.
The unmodified path will be used, when rewrite function returns undefined
const { createProxyMiddleware } = require('http-proxy-middleware');
const rewriteFn = function (path, req) {
return path.replace('/api/foo', '/api/bar');
};
const options = {
target: 'http://localhost:3000',
pathRewrite: rewriteFn,
};
const apiProxy = createProxyMiddleware('/api', options);
// `/api/foo/lorum/ipsum` -> `http://localhost:3000/api/bar/lorum/ipsum`