lwsjs / local-web-server

A lean, modular web server for rapid full-stack development.
MIT License
1.2k stars 85 forks source link

Errors when trying to redirect to remote URL #152

Closed paul-pro closed 3 years ago

paul-pro commented 3 years ago

Hi! I was trying to establish a local server for a built version of my application with a lot of api requests (so a lot of redirects). Most of the requests use relative URL and when I try to redirect them with --rewrite '/app-name/(.*)-service/(.*)/ -> https://domain.com/app-name/$1-service/$2' config I get the following error: Error: Protocol missing from request seems like utils.getTargetUrl called with this relative URL and rewrite.to ends up being relative as well (despite provided full remote URL). Do I miss something in my config or local-web-server should append base to the relative URLs?

Thanks!

75lb commented 3 years ago

Hi, could you give me an example of a URL which does not redirect correctly? I did a quick check using the redirect debugging tool, the output looks ok:

$ npx lws-rewrite '/app-name/(.*)-service/(.*)/' 'https://domain.com/app-name/$1-service/$2' /app-name/test-service/test/
https://domain.com/app-name/test-service/test
paul-pro commented 3 years ago

Thank you for the quick answer! When I am modifying lib sources like image I am seeing remoteUrl is /app-name/test-service/test (instead of expected https://domain.com/app-name/test-service/test)

75lb commented 3 years ago

what is the ctx.url value passed to that util.getTargetUrl invocation? (the third arg)

paul-pro commented 3 years ago

Thanks, I was able to track down that the actual request URL has no slash / at the end and the rewrite rule has one, so my regExp was returning empty fromMatches -> returning initial URL itself, instead of replaced remote URL.

I was confused with this rule triggering at all (and then failing to create regExp), but still thanks a lot for you help

75lb commented 3 years ago

ok, no worries.. hopefully the debugging tool can help you develop and test your rewrite expressions in future 👍