Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 384
Picci Avatar asked Sep 28 '16 10:09

Picci


People also ask

How do I serve HTML files in node?

The open source text editor Brackets also includes a NodeJS static web server. Just open any HTML file in Brackets, press "Live Preview" and it starts a static server and opens your browser at the page. The browser will auto refresh whenever you edit and save the HTML file.

How Pass value from node JS to HTML?

If your node server also serves this HTML page, then you can use a relative path to point to your route like this: action="/handle-form-data" . The input tag nested inside the form is used to collect user input. You have to assign a name property to your data so that you can recognize this piece of data on the server.

What is HTTP createServer in node JS?

The http. createServer() method turns your computer into an HTTP server. The http. createServer() method creates an HTTP Server object. The HTTP Server object can listen to ports on your computer and execute a function, a requestListener, each time a request is made.


2 Answers

Use as specified in the documentation.

http-server --port 8080 -P http://localhost:8080?

Note the ? at the end of the proxy URL.

like image 156
Farkhod Daniyarov Avatar answered Sep 19 '22 11:09

Farkhod Daniyarov


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

like image 33
Gabriel Avatar answered Sep 22 '22 11:09

Gabriel