Narrow down which requests should be proxied. The path
used for filtering is the request.url
pathname. In Express, this is the path
relative to the mount-point of the proxy.
pathFilter
is optional and is useful in cases where you are not able to use the regular middleware mounting.
http-proxy-middleware
offers several ways to do this:
This will match paths starting with /api
const { createProxyMiddleware } = require('http-proxy-middleware');
const apiProxy = createProxyMiddleware({
target: 'http://localhost:3000',
pathFilter: '/api',
});
// `/api/foo/bar` -> `http://localhost:3000/api/foo/bar`
This will match paths starting with /api
or /rest
const { createProxyMiddleware } = require('http-proxy-middleware');
const apiProxy = createProxyMiddleware({
target: 'http://localhost:3000',
pathFilter: ['/api', '/rest'],
});
// `/api/foo/bar` -> `http://localhost:3000/api/foo/bar`
// `/rest/lorum/ipsum` -> `http://localhost:3000/rest/lorum/ipsum`
This will match paths starting with /api/
and should also end with .json
const { createProxyMiddleware } = require('http-proxy-middleware');
const apiProxy = createProxyMiddleware({
target: 'http://localhost:3000',
pathFilter: '/api/**/*.json',
});
Multiple wildcards can be used.
const { createProxyMiddleware } = require('http-proxy-middleware');
const apiProxy = createProxyMiddleware({
target: 'http://localhost:3000',
pathFilter: ['/api/**/*.json', '/rest/**'],
});
This example will create a proxy with globs.
const { createProxyMiddleware } = require('http-proxy-middleware');
const apiProxy = createProxyMiddleware({
target: 'http://localhost:3000',
pathFilter: ['foo/*.js', '!bar.js'],
});
Write your custom pathFilter
function to have full control on the matching behavior.
The request pathname
and req
object are provided to determine which requests should be proxied or not.
const { createProxyMiddleware } = require('http-proxy-middleware');
const pathFilter = function (pathname, req) {
return pathname.match('^/api') && req.method === 'GET';
};
const apiProxy = createProxyMiddleware({
pathFilter: pathFilter,
target: 'http://localhost:3000',
});