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
import { createProxyMiddleware } from '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
import { createProxyMiddleware } from '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
import { createProxyMiddleware } from 'http-proxy-middleware';
const apiProxy = createProxyMiddleware({
target: 'http://localhost:3000',
pathFilter: '/api/**/*.json',
});Multiple wildcards can be used.
import { createProxyMiddleware } from 'http-proxy-middleware';
const apiProxy = createProxyMiddleware({
target: 'http://localhost:3000',
pathFilter: ['/api/**/*.json', '/rest/**'],
});This example will create a proxy with globs.
import { createProxyMiddleware } from '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.
import { createProxyMiddleware } from 'http-proxy-middleware';
const pathFilter = function (pathname, req) {
return pathname.match('^/api') && req.method === 'GET';
};
const apiProxy = createProxyMiddleware({
pathFilter: pathFilter,
target: 'http://localhost:3000',
});