112

I have an HTML file and I run it on localhost. But, this file includes a mirror using a webcam. For example, how can I run this HTML file on localhost? Webcam starts in this example when checking to live checkbox.

Rishon_JR
  • 160
  • 11
Volkan Şahin
  • 1,181
  • 2
  • 10
  • 15
  • 1
    With LiveServer extension on [mac](https://www.youtube.com/watch?v=S_iPfU3wK2Q) on [windows](https://www.youtube.com/watch?v=Wd7cVmtiFUU) – InfiniteStack Sep 21 '22 at 14:08

12 Answers12

225

You can run your file in http-server.

1> Have Node.js installed in your system.

2> In CMD, run the command npm install http-server -g

3> Navigate to the specific path of your file folder in CMD and run the command http-server

4> Go to your browser and type localhost:8080. Your Application should run

Rishon_JR
  • 160
  • 11
Clinton Roy
  • 2,659
  • 2
  • 10
  • 7
111

You can use python -m http.server. By default the local server will run on port 8000. If you would like to change this, simply add the port number python -m http.server 1234

If you are using python 2 (instead of 3), the equivalent command is python -m SimpleHTTPServer

DBolton
  • 506
  • 4
  • 9
Nora
  • 3,093
  • 2
  • 20
  • 22
  • 5
    I create HTTPServer. What will i do now? – Volkan Şahin Jul 21 '16 at 08:11
  • 2
    OK. I am done. I transport my project to C:/Python27 and i run it on http://127.0.0.1:8000/ – Volkan Şahin Jul 21 '16 at 08:14
  • 3
    So you run the python command "python -m SimpleHTTPServer" and then navigate to http://localhost:8000/ in your browser and click the desired html file. Great solution! – Gal_M Jun 09 '18 at 17:39
  • 10
    The python 3 equivalent is `python3 -m http.server` You want to navigate to the directory that contains the files you wish to open locally, and then run that command. Then open up localhost:8000 if you are using the default port. – Mdomin45 Dec 01 '18 at 19:45
19

If you are running Python3, you may want to instead try:

python -m http.server

See this answer.

MrE
  • 290
  • 3
  • 5
17

If you have Node.js installed then from the folder you want to share you can simply run:

npx http-server

To add CORS you can run:

npx http-server --cors
sax
  • 808
  • 1
  • 12
  • 25
9
  • Install Node js - https://nodejs.org/en/

  • go to folder where you have html file:

    • In CMD, run the command to install http server- npm install http-server -g
    • To load file in the browser run - http-server
  • If you have specific html file. Run following command in CMD.- http-server fileName

  • by default port is 8080

  • Go to your browser and type localhost:8080. Your Application should run there.

  • If you want to run on different port: http-server fileName -p 9000

Note : To run your .js file run: node fileName.js

Ritu Gupta
  • 2,249
  • 1
  • 18
  • 11
9

On macOS:

Open Terminal (or iTerm) install Homebrew then run brew install live-server and run live-server.

You also can install Python 3 and run python3 -m http.server PORT.

On Windows:

If you have VS Code installed open it and install extension liveserver, then click Go Live in the bottom right corner.

Alternatively you can install WSL2 and follow the macOS steps via apt (sudo apt-get).

On Linux:

Open your favorite terminal emulator and follow the macOS steps via apt (sudo apt-get).

  • Your VSCode suggestion was perfect for me. I was already using VSCode and I just needed a way to host a demo site locally for me to play around. The Go Live extension is a perfect one-click solution – Raam Kumar Jan 08 '23 at 14:43
5

As Nora suggests, you can use the python simple server. Navigate to the folder from which you want to serve your html page, then execute python -m SimpleHTTPServer. Now you can use your web-browser and navigate to http://localhost:8000/ where your page is being served. If your page is named index.html then the server automatically loads that for you. If you want to access any other page, you'll need to browse to http://localhost:8000/{your page name}

3

You can try installing one of the following localhost softwares:

  1. xampp
  2. wamp
  3. ammps server
  4. laragon

There are many more such softwares but the best among them are the ones mentioned above. they also allow domain names (for example: example.com)

2

Another option if you have Node.js installed,

npx serve .

It will serve you a static site/single page application in the current directory.

Reference

Philip
  • 3,135
  • 2
  • 29
  • 43
1

You can install Xampp and run apache serve and place your file to www folder and access your file at localhost/{file name} or simply at localhost if your file is named index.html

HamzaMushtaq
  • 1,660
  • 13
  • 13
1

You can also use PHP to server the files in http

make sure you installed PHP, run the below command to verify

php --version

if PHP is not installed run below command to install it

sudo apt install php7.4-cli

Once after the installation go to the file path and execute the below command in the terminal

php -S localhost:8000
1

just npx serve it's more compatible with esmodule

sema toghem
  • 77
  • 1
  • 4