i hope the serve can add proxy feture
eg: when request /api/users,serve can proxy to other server
Thank you for the effort you've put into this!
This is now possible by supplying a serve.json file. All thanks to serve-handler, as described in the notes for the new release.
Simply add redirects or rewrites to your config file, as described in the serve-handler documentation.
Those features are a little different than proxying though, right? I might be missing something, but I don't see a way with rewrites or redirects to actually serve responses from another server without returning a 301 or 302. It would be nice to be able to do something similar to an nginx reverse proxy.
It seems this has actually been discussed and started here:
Yes, @leo please take a look at #75 which is a proxy implementation. A yes/no would be helpful. I'd like to maintain a public fork (currently using a private fork in production) if there's no interest in merging.
In my case I needed rewrites, and I must say trying to understand how this whole thing works is not easy. I couldn't figure out without looking into the source code. First I learned that serve.json's path is resolved relative to the target (not current) directory. But the trickiest thing was specifying rewrites. It's not easy, unless there's an example for your case in the documentation. How exactly path-to-regexp and minimatch interact with each other, and all. Now I see that it's either many-to-one with minimatch, or one-to-one with path-to-regexp.
But in my case I needed many-to-many :) I needed to prefix the urls. That is, I wanted serve assets to do the following:
/assets/index.js -> /index.js
/assets/css/style.css -> /css/style.css
...
Like, /assets/(.*) -> /$1. One might say, I could just do serve ., but that would have made the assets' directory siblings available. The closest I could get to my goal is:
{"rewrites": [
{"source": "/assets/:path*", "destination": "/:path"}
]}
I was able get away with /assets/:path* owing to this line. It seems like it serves another purpose, but /assets/:path* gets transformed into /assets/:path(.*), which is okay as long as path-to-regexp is concerned. The issue is with destination. When :path is interpolated, it is also encoded. So my /'s become %2F's, and apparently it doesn't work:
/assets/css/style.css -> /css%2Fstyle.css
Most helpful comment
Thank you for the effort you've put into this!
This is now possible by supplying a
serve.jsonfile. All thanks toserve-handler, as described in the notes for the new release.Simply add
redirectsorrewritesto your config file, as described in theserve-handlerdocumentation.