node http-server to respond with index.html to any request

Picci picture Picci · Sep 28, 2016 · Viewed 15.7k times · Source

I have installed http-server globally.

I launch it from myDir on localhost port 8080. In myDir I have index.html.

If I request (from the browser) http://localhost:8080/ I get index.html, which is OK.

If I request though http://localhost:8080/anything I do not get any response from the server.

What I would like, on the contrary, is that my server always responds with index.html to any http request reaching localhost on port 8080.

Is this possible.

Thanks in advance

Answer

Gabriel picture Gabriel · May 11, 2017

To achieve what you are asking for, I recommend you live-server instead of http-server.

live-server --port=8080 --entry-file=./index.html

live-server is also providing a hot-reload but it was not one of your request

Edit: live-server is not designed to be used in production. No gzip compression for example

Edit 2: The maintainer of http-server clearly said in this comment that never, http-server will consider the SPA use-cases

Edit 3: serve seems to be a good option too