Skip to content

Latest commit

 

History

History
102 lines (71 loc) · 2.59 KB

pathFilter.md

File metadata and controls

102 lines (71 loc) · 2.59 KB

Path Filter

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:

Path

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`

Multi Path

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`

Wildcard

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',
});

Multi Wildcard

Multiple wildcards can be used.

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

const apiProxy = createProxyMiddleware({
  target: 'http://localhost:3000',
  pathFilter: ['/api/**/*.json', '/rest/**'],
});

Wildcard / Exclusion

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'],
});

Custom filtering

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',
});