How to run html file on localhost? - javascript

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.

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

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

If you are running Python3, you may want to instead try:
python -m http.server
See this answer.

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

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

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).

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}

You can try installing one of the following localhost softwares:
xampp
wamp
ammps server
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)

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

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

just npx serve it's more compatible with esmodule

Related

How to create NodeJS server using http-server?

I am new to Nodejs and would like to know how to use http server using the modejs http-server module.
I did the following steps:
1. Installed Nodejs.
2. Installed http-server module using the command : npm install http-server -g
3. Created a folder with a HTML doc
Once I start the server using the command http-server, the URL provides the following output:
192.168.1.5 sent an invalid response
CMD commmand :
C:\Users\user\folderLocation>http-server
HTML output :
192.168.1.5 sent an invalid response
Pls. do help with with this issue and provide feedback with what i'm doing wrong.
Thanks in advance!!
http-server -p8080 . //serves current directory
http-server -p8080 ./public //serves directory named public
https://www.npmjs.com/package/http-server
Apparently there is a bug in a dependency of the recent version of http-server. One solution is to install http-server#0.9.0

Deploying static js and html file on web server using http-server and forever

I have a folder on ubuntu server which has a bunch of d3, js files. I want to display the html file on the browser running on the server.
I have got both node.js and http-server and forever installed.
I tried first with just http-server using nohup to run it in background.
nohup http-server -p 8000 -a 10.1.152.145 &
THis works and open up the directory structure on the browser. On clicking the html file there it opens up the visualization. I however want the html to be directly displayed on browser without havinng to click on it.
I then read forever is a better way to run a code in background on server so I did this:
forever start $(which http-server) -p 8000 -a 10.1.152.145 -d false
This doesn't give any error in bash, but when I got to
10.1.152.145:8000 on browser it doesn't display any file directory and says access denied.
Access to 10.1.152.145 was denied You don't have authorization to view this page.
HTTP ERROR 403
Any help in fixing this?
Thanks

Http-server running in background with nohup doesn't remain live

I have an ubuntu server. On which I have transferred some files which have some js and html code.
I used http-server from node.js to start a web server so that I can display the html page on the server.
I used nohup so that it can remain running even if I disconnect or close my system.
Here is the command I used:
nohup http-server -p 8000 -a 10.4.145.182 &
Now this helps me visualize the files on 10.4.145.182:8000 but I am noticing after sometime the server goes down and one can't access the html page on that ip in their browser.
I thought nohup helps run things in background even if one closes their system or logs out of server.
How do I make this web server running always then and accessible to url to everyone
Thanks
EDIT:
As per the suggestion below of using pm2, I installed latest version of node and then started service with pm2.
It says service started for http-server but when I go the to ip with port 8000 on browser it doesn't open up.
Here is the command I ran in my directory which has the html and d3 files.
$ pm2 start $(which http-server) -p 8000
And here is the output in shell of the pm2
Try this:
http-server & exit
You can also specify a port number:
http-server -p 8082 & exit
Reference here
I suggest you use pm2 . Long story short , check the official link https://www.npmjs.com/package/pm2
Steps :
Install :
npm install pm2 -g
Run your app :
pm2 start app.js
In your case use :
pm2 start /usr/local/bin/http-server -- -p 8080
for reboot to work run :
pm2 startup systemd
take the last line , change the user and home path and run the modified line as SUDO !!! you need sudo access for this !!!

Run Geany as root

I have ubuntu 16.04 and I installed Geany using Ubuntu Software Centre. I need to edit some php and js files which happen to be in root directories. So when I try to save the changes in Geany, it generates following error:
Error opening file '/var/www/html/project1/team/team.php': Permission denied
The file on disk may now be truncated!
How can I make Geany to edit and save these files?
try
gksudo geany
Use gksudo with graphical apps written in GTK+ instead of sudo
sudo -Eb geany
worked for me on Debian 10 with xrdp. I executed the command on xrdp desktop Terminal Emulator.
-E: preserve user environment when running command
-b: run command in the background
Note that without -b option the control will not return to terminal until geany is closed.
Not an expert on this, was just trying to see sudo's options and this did the trick!
sudo geany
but you really shouldn't edit your code in production environment
if you are developing locally navigate to your www file using terminal
after that you have to change the permission to 777 using this command
sudo chmod 777 html
which mean every one can read-write-execute your files
REMEMBER THIS IS FOR LOCALLY DEVELOPING

Is there a simple html-webserver for mac?

Sometimes when I do front-end development, I use MAMP as a web-server so that I don't have to deal with CORS-errors. Since I upgraded to mavericks MAMP won't work any more and it is pretty heavy. Is there something simpler or more lightweight built in or easily installed?
http-server
Install:
npm install http-server -g
Start:
http-server -p port -a address
depending on how simple you like it:
Python 2:
python -m SimpleHTTPServer 8000
https://docs.python.org/2/library/simplehttpserver.html
Python 3:
python3 -m http.server
https://docs.python.org/3.3/library/http.server.html
XAMPP is a well-known local webserver. I'm told it works quite similar to MAMP
Navigate to the folder you want to open and write:
python -m SimpleHTTPServer 8000
8000 is the port, you may change it.
The terminal will respond with something like Serving HTTP on 0.0.0.0 port 8000 ...
if you navigate to the adress 0.0.0.0:8000 you will see your webpage.
php -S localhost:3000
Or the port you want.

Categories