http-party / http-server

a simple zero-configuration command-line http server

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fallback proxy doesn't work on Node v17+

mirismaili opened this issue · comments

Environment Versions

  1. OS Type: Windows 11
  2. Node version: 16.14.2 & 17.9.0 & 18.0.0 & 18.2.0
  3. http-server version: 14.1.0

Steps to reproduce

http-server --proxy http://localhost:8080? doesn't redirect deep paths to the root.

I use NVM. When I switch to node.js v16.14.2 everything works well. But not on v17.9.0, v18.0.0 and v18.2.0.

Expected result

[2022-05-22T23:06:05.765Z]  "GET /en/org" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36"
(node:2160) [DEP0066] DeprecationWarning: OutgoingMessage.prototype._headers is deprecated
(Use `node --trace-deprecation ...` to show where the warning was created)
[2022-05-22T23:06:05.780Z]  "GET /?/en/org" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36"
[2022-05-22T23:06:05.863Z]  "GET /env-config.js" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36"
[2022-05-22T23:06:05.868Z]  "GET /?/env-config.js" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36"
[2022-05-22T23:06:06.027Z]  "GET /static/js/main.bundle.js" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36"
...

Actual result

[2022-05-22T22:50:53.564Z]  "GET /en/org" Error (404): "connect ECONNREFUSED ::1:8080"
[2022-05-22T22:50:53.564Z]  "GET /en/org" Error (404): "Not found"
[2022-05-22T22:50:53.566Z]  "GET /en/org" Error (404): "Not found"

Having a similar problem, but even trying node v16.14.2 doesnt solve it for me.
React SPA, home page and routes work fine, but browser refresh or deep linking fails.
Proxy functionality seems not to intercept, returning 404 instead.

http-server 14.1.1 (latest)
node 18.12.1 (upgraded to latest)
Ubuntu Server Linux 20.04.5, all packages latest

(Using TLS because browser requires it for Brotli support, but happens in http too.)

$ node_modules/.bin/http-server -S -C ~/keys/localhost.crt -K ~/keys/localhost.key ./dist -p 3000 -b --proxy https://localhost:3000?
Starting up http-server, serving ./dist through https

http-server version: 14.1.1

http-server settings:
CORS: disabled
Cache: 3600 seconds
Connection Timeout: 120 seconds
Directory Listings: visible
AutoIndex: visible
Serve GZIP Files: false
Serve Brotli Files: true
Default File Extension: none

Available on:
  https://192.168.1.151:3000
  https://127.0.0.1:3000
Unhandled requests will be served from: https://localhost:3000?
Hit CTRL-C to stop the server

[2022-11-18T21:09:39.101Z]  "GET /" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
(node:7448) [DEP0066] DeprecationWarning: OutgoingMessage.prototype._headers is deprecated
(Use `node --trace-deprecation ...` to show where the warning was created)
[2022-11-18T21:09:39.124Z]  "GET /index.c9b5de754a3ba5d92a27.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-18T21:09:39.281Z]  "GET /613.063e62b8ec900d7dd795.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-18T21:09:39.283Z]  "GET /613.063e62b8ec900d7dd795.bundle.js" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-18T21:09:39.285Z]  "GET /favico.ico" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-18T21:09:47.624Z]  "GET /712.f12e0002bb4a36858b9a.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-18T21:09:47.627Z]  "GET /712.f12e0002bb4a36858b9a.bundle.js" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-18T21:09:47.630Z]  "GET /favico.ico" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-18T21:09:47.638Z]  "GET /62a09c720252e5bac0ae.png" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-18T21:09:47.640Z]  "GET /af7f4744cd790b3c6e34.png" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-18T21:09:47.642Z]  "GET /2e31057ae44497e0448c.png" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-18T21:09:47.645Z]  "GET /346ae7a75db4aa5ccf18.png" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-18T21:09:47.648Z]  "GET /6851542b510b90d895e0.png" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"

<browser refresh here, proxy will now fail>

[2022-11-18T21:09:51.047Z]  "GET /series/classic-series" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-18T21:09:51.054Z]  "GET /series/classic-series" Error (404): "connect ECONNREFUSED ::1:3000"
[2022-11-18T21:09:51.055Z]  "GET /series/classic-series" Error (404): "Not found"
[2022-11-18T21:09:51.055Z]  "GET /series/classic-series" Error (404): "Not found"

PSA: I ended up using sirv-cli with the --single flag as a workaround.

Running into the same problem with http-server. Tried sirv-cli, but it did not work for me (does not play well when starting multiple web servers via node-foreman). Using browser-sync as workaround.

As a workaround you can try using

http-server --proxy=http://127.0.0.1:8080/index.html?

or you can try

http-server --proxy=http://localhost:8080/index.html? -a=localhost

Messages from http-server output says:

Available on:
  https://127.0.0.1:8080
Unhandled requests will be served from: http://localhost:8080?
...
[2022-05-22T22:50:53.564Z]  "GET /en/org" Error (404): "connect ECONNREFUSED ::1:8080"

meaning that http-server is not listening on ipv6 address: ::1 but only on ipv4 127.0.0.1

This set of options finally worked for me while using Node 18:

http-server ./build -a localhost -P http://localhost:3002? -p 3002

Available on:
  http://localhost:3002
Unhandled requests will be served from: http://localhost:3002?