serve-handler icon indicating copy to clipboard operation
serve-handler copied to clipboard

Support for rewriting to external URLs

Open chiefjester opened this issue 6 years ago • 12 comments

Would be great to have proxy support for external APIs in this microservice world. This would also alleviate the pain of using CORs. Also in relation to issue #28

{
  "proxy": [
    { "path": "api/**", "destination": "https://xxx.api/" }
  ]
}

chiefjester avatar Jun 15 '18 17:06 chiefjester

This is super interesting, thanks for proposing it. The only caveat is that the space of possible configuration options for such a feature is very broad.

rauchg avatar Jun 15 '18 17:06 rauchg

cc @simonedavico

rauchg avatar Jun 15 '18 17:06 rauchg

Maybe we could look at how others do it to be sure of taking into account every option? As I mentioned in #28, light-server offers a similar feature :)

simonedavico avatar Jun 15 '18 17:06 simonedavico

This all sounds great. However, we don't have an internal need for this.

So any help would be greatly appreciated! ☺️

leo avatar Aug 21 '18 10:08 leo

It'd be great to see this feature indeed, especially configurable inside serve as a CLI argument.

Meanwhile, my workaround is to use http-server:

npm install --global http-server
http-server path/to/web --port 5000 --proxy https://example.com/

This is how I could proxy http://localhost:5000/api/call to https://example.com/api/call.

kachkaev avatar Oct 17 '18 16:10 kachkaev

@leo, any comment about @nathancahill PR?

@JounQin I'm not investing more time until I hear back from the maintainers that they are interested (in which case this PR can be finished) or not interested (in which case the feature will be available in serve-proxied on npm).

https://github.com/zeit/serve-handler/pull/75

Toub avatar Sep 25 '19 12:09 Toub

I need this as well. For my use case I'm emulating locally my server's proxy settings. Hoping something like this would work:

"rewrites": [
     { "source": "/game/main", "destination": "http://localhost:9000/main"}
  ]

geddski avatar Nov 20 '19 22:11 geddski

Any updates? this pr #75 is waiting for approvals

FerLuisxd avatar Apr 19 '20 06:04 FerLuisxd

I was using serve for my SPA until I couldn't anymore because I need to proxy to API server. I found that local-web-server seems to support a lot of scenarios and did the trick for me.

Example command:

ws --port 5000 --directory dist --spa app.html --rewrite "/api/(.*) -> http://localhost:3000/$1"

Opens local server on port 5000, serves the dist directory, uses app.html as the SPA index, and rewrites requests to /api/$1 as http://localhost:3000/$1

valentinoli avatar Nov 05 '20 22:11 valentinoli

I need this feature also.

abronin avatar Jan 18 '21 19:01 abronin

shameless self-promotion alert:
this feature is available in my @warren-bank/serve fork of serve

notes about using redirect rules:

  • require an engine attribute..
    which permits you to choose which methodology/library is used to:
    • match the url pathname against a pattern
    • replace capture groups
  • an optional boolean proxy attribute can be used to:
    • download from an external url
    • pipe the response back to the client

warren-bank avatar Feb 16 '22 03:02 warren-bank

What date is today ? Why is this issue still open ?

kmsheng avatar Aug 18 '23 06:08 kmsheng