fastify / fastify-http-proxy

Proxy your http requests to another server, with hooks.
MIT License
334 stars 89 forks source link

Match by suffix (instead of prefix) #357

Open peterbe opened 1 month ago

peterbe commented 1 month ago

Prerequisites

🚀 Feature Proposal

In porting an Express + http-proxy-middleware app, there's something I can't do; match by suffix rather than prefix. My Express code looks like this:

app.use("*/ping", backendProxy);

That means that any URL like /foo/ping or /bla/ble/blu/ping would be proxied away from this to that backend it was configured to.

This would be nice to be able to do with fastify-http-proxy. (Perhaps it's possible with some hacks, but I can't figure it out)

Motivation

Another use-case other than the one above is if you wanted to match by file extension. E.g.

server.register(require('@fastify/http-proxy'), {
  upstream: 'http://my-api.example.com',
  pattern: '*.webp'
});

Example

server.register(require('@fastify/http-proxy'), {
  upstream: 'http://my-api.example.com',
  pattern: '*/info'
});

or

server.register(require('@fastify/http-proxy'), {
  upstream: 'http://my-api.example.com',
  suffix: '/info'
});

it would match http://localhost:3000/anything/info or http://localhost:3000/a/b/c/d/info

jsumners commented 1 month ago

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

mcollina commented 1 month ago

I don't think this would be feasible.