Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The question mark in the proxy example in the doc -P http://localhost:8080? is causing issue #645

Closed
lz000 opened this issue Aug 11, 2020 · 7 comments
Labels
as-intended Behavior described is intentional

Comments

@lz000
Copy link

lz000 commented Aug 11, 2020

Steps to reproduce the issue, if applicable. Include the actual command and output and/or stack trace.

I ran http-server /data -p 80 -P http://localhost:8080? as suggested in the doc. When I hit http://localhost/test, my downstream server (which listening on 8080) get /?/test

What did you expect to happen?

Maybe I don't understand the purpose of the ? at the end. Removing the ? fix the issue. Is this a mistake in the doc or I missed anything?

Tell us about your environment

  • exact http-server version: 0.12.3
  • Node version: 12.18.3
  • Platform: Windows 10
@lz000 lz000 changed the title The question mark in the proxy example in the doc doc -P http://localhost:8080? is causing issue The question mark in the proxy example in the doc -P http://localhost:8080? is causing issue Aug 11, 2020
@elwinschmitz
Copy link

elwinschmitz commented Sep 8, 2020

I think I had the same misunderstanding of that section in the README; As I thought it was explaining the use of the --proxy-option, but actually it isn't. (See the discussion: #338 (comment))

That section in the README tries to explain a clever 'workaround' for when you want to test a Single-Page-App which handles its own urls (like Angular/React/Vue can with their Router-components).

The use-case you are looking for (similar to mine, I think), is to run your static front-end on http://loocalhost:80 and let all requests to /api/ for example, go to http://back-end.example.com/api/ (or locally to http://localhost:8080/api/)

To do that, you would be using 'just' the --proxy-option:
npx http-server --port 80 --proxy http://back-end.example.com/

The ? in the URL in the README is only there to 'pass' the rest of the URL to your local index.html and let your framework's Router handle it.

@thornjad
Copy link
Member

@elwinschmitz is correct, that part of the docs is only a suggestion for a workaround for single-page apps, which http-server does not explicitly support (and won't). To use the proxy normally, you probably don't want the ?.

@thornjad thornjad added the as-intended Behavior described is intentional label Oct 13, 2020
@rook2pawn
Copy link

I think I had the same misunderstanding of that section in the README; As I thought it was explaining the use of the --proxy-option, but actually it isn't. (See the discussion: #338 (comment))

That section in the README tries to explain a clever 'workaround' for when you want to test a Single-Page-App which handles its own urls (like Angular/React/Vue can with their Router-components).

The use-case you are looking for (similar to mine, I think), is to run your static front-end on http://loocalhost:80 and let all requests to /api/ for example, go to http://back-end.example.com/api/ (or locally to http://localhost:8080/api/)

To do that, you would be using 'just' the --proxy-option: npx http-server --port 80 --proxy http://back-end.example.com/

The ? in the URL in the README is only there to 'pass' the rest of the URL to your local index.html and let your framework's Router handle it.

@elwinschmitz I have a SPA and i need this kind of setup to make my SPA work with http-server

http-server ./web -o -c-1 -p 8080 --proxy http://localhost:8080?

however I also have a backend on 5150 and want to proxy /api to 5150.. Basically I want to use both the catch-all redirect for SPA but also a proxy to the backend to another port for anything request like /api/. I tried looking at the options in --proxy-options and couldn't figure out how to do this. You said you used the proxy to pass to your BE - I assume you weren't needing a catchall redirect for SPA on your FE?

@elwinschmitz
Copy link

@rook2pawn I only use it for the 'fake' backend indeed.

But I think you can run 2 instances of http-server next to each other, with an & between the commands.
Or with a tool like npm-run-all, to run 2 tasks in parallel.

@rook2pawn
Copy link

rook2pawn commented Nov 7, 2021

@elwinschmitz I was very happy with the solution i am using now and i posted it here. Essentially I set up a vanilla https server, and then proxy everything that's starts with /api/ to the BE server, and everything else proxies to the http-server for my SPA FE server. It works well and wanted to document that somewhere. What's neat is that I can now have interesting features like rewriting, load balance on my BE and even things that could be done on the BE that are better done in the proxy like blacklist and simple ddos protection. I was really happy that I didn't hit any major snags in my setup. My actual code rewrite the URL to remove the /api/ specification, and then I simply specify on my cookies Path=/ so they won't get set to Path=/api in my BE.

@elwinschmitz
Copy link

Aha. That's sounds neat.
I am using this in my local development setup only, so any 'real usage'/load/security concerns are 'out of scope' for me. 😉

@rook2pawn
Copy link

rook2pawn commented Dec 18, 2021

diagram is the basic setup that i use - what's nice about this approach is that you have a clean separation between your SPA and your BE (repo, hosting/instances) and you can even put all sorts of things at your proxy level (administrative redirects, prevent spam attacks) so your logic on your SPA/BE/proxy are all cleanly split into the domains they are best suited for. i setup a small repo that ties this all together but you can do this setup on any framework you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as-intended Behavior described is intentional
Projects
None yet
Development

No branches or pull requests

4 participants