12

I have created a basic AngularJS app in node environment. I am using http-server module for serving the files to browser. Everything is working fine except for the fact that I can't get to serve index.html by default when the server launches.

I checked out npm registry for more options to http server module, and also tried looking for a relevant question on SO but still not able to get what I desire.

Is it possible at all to specify the file to pick up while server starts.

My server basically starts at localhost:8080 while I would like localhost:8080/index.html

My start script is http-server -a localhost -p 8080 -c-1. If I do something like http-server -a localhost -p 8080 -c-1 index.html, to my surprise it opens the index.html file but serves it on file protocol and not on localhost.

What am I doing wrong here.

P.S. I visited Angular JS seed and there official example says http-server -a localhost -p 8080 -c-1 ./app. However, when I do this I get error Windows can't find specified path, although my structure is similar to the seed.

My structure:

dir
   --app.js
   --index.html
   --node_modules
   --package.json
Saurabh Tiwari
  • 4,632
  • 9
  • 42
  • 82

4 Answers4

6

Make sure you are building your project first and generating an output ./dist folder or similar.

Then try this command instead:

http-server -p 8080 ./dist -o http://localhost:8080/index.html

You are missing the http:// in your url.

Elliott
  • 325
  • 2
  • 13
0

Add a -f /index.html or whatever the document is. In my case for example:

http-server -c-1 -f /index.html
xiawi
  • 1,772
  • 4
  • 19
  • 21
Chigo Kawa
  • 117
  • 1
  • 4
0

For me what solved the problem was, I clicked the link through the CLI after doing http-server [path].

In my case I ran http-server . (which has an index.html in the directory), then logs out this.

Starting up http-server, serving .

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: false
Default File Extension: none

Available on:
  http://192.168.8.133:8080
  http://127.0.0.1:8080
  http://172.23.128.1:8080
Hit CTRL-C to stop the server

Now I just used the link in the line below Available on: which is:

  1. http://192.168.8.133:8080, works for me
  2. http://127.0.0.1:8080, did not work for me, it download file instead
  3. http://172.23.128.1:8080, works!

Such a weird behaviour of http-server.

Irfandy Jip
  • 1,308
  • 1
  • 18
  • 36
-2

Try to put you static file inside a new directory public. and run you server http-server

- app.js
-public
--index.html
-package.json
Lee Goddard
  • 10,680
  • 4
  • 46
  • 63
PPB
  • 2,937
  • 3
  • 17
  • 12
  • Did you restarted your server ? It will server default index.html file inside public dir and should work on both URL localhost:8080/index.html or localhost:8080 – PPB Apr 23 '17 at 12:14
  • Yes I restarted my server. I hope you understand my question, I can get my application to work on localhost:8080/index.html by manually entering this url. What I want is it should itself open the browser at this url. – Saurabh Tiwari Apr 23 '17 at 12:17
  • Okay Got It :) Something similar here https://github.com/indexzero/http-server/issues/231 – PPB Apr 23 '17 at 12:54
  • This thread seems to address the same concern as I have, but if I try to use `-o localhost:8080/index.html` I get a error from Windows saying `The associated program is not installed` – Saurabh Tiwari Apr 23 '17 at 13:08
  • https://github.com/indexzero/http-server/compare/master...craigmichaelmartin:master If you want you can try this fork. I think this contain some fix for the same issue – PPB Apr 23 '17 at 13:13
  • I saw the provided code, it basically adds the argument to the opener url. As per the code `-o /index.html` should work fine, however this doesn't seem to be the case. The url is checked based on `typeof argv.o` so I tried using command as `http-server -o '/index.html' ". But even this doesn't wrk. Strange!!! – Saurabh Tiwari Apr 23 '17 at 13:39