Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a html file on localhost (any port)

I am creating a website using HTML, CSS and js with java for server side. I need to run it on localhost.

Extra info: I am using mac os x 11 (el capitan)

I have already tried python and node.js but it clashes with java

So i need a technology that won't clash with java and will help me localhost on mac

like image 420
sidrao2006 Avatar asked Jun 22 '19 16:06

sidrao2006


People also ask

How do I run a localhost file?

Running your code on localhostType the command php -S localhost:8000 to run your site on port 8000. Note: If you get an error that 'php' is not recognized, you likely will need to add it to your path manually. To do that, locate php.exe (for me it is in the directory C:\xampp\php\ ).

How connect HTML to xampp?

For this, you need to create a working folder first and then create a web page with the name “contact. html”. If you install xampp your working folder is in folder this “E:\xampp\htdocs”. You can create a new folder “contact” on your localhost working folder.

What is localhost HTML?

Localhost is the url on a computer that points to itself. The computer does not need the internet for the address to work since it only checks on itself. To access localhost, you write localhost or 127.0. 0.1 on the browser.


2 Answers

You can use caddy

Install: brew install caddy

To serve static files from the current working directory, run:

caddy file-server --browse --listen :2015

Caddy's default port is 2015, so open your browser to http://localhost:2015.

like image 72
jwillker Avatar answered Oct 10 '22 20:10

jwillker


IIRC macOS comes with PHP preinstalled, and PHP has built-in web-server which should be enough for serving static content.

So, open Terminal.app and then:

cd your/project/dir
php -S localhost:8080

After than you can navigate to http://localhost:8080/ and see your site in the browser (given you have index.html in your project, otherwise there will be "Not Found" message).

There are more advanced and/or less terminal-oriented ways, of course, but since you already tinkering with python and node, another terminal command should not be a problem.

BTW, you might want to look at that terminal window from time to time, as it outputs nice log of what things were requested from server. Good if you want to check for invalid references, 404 errors, etc. Here is a sample output:

$ php -S localhost:8080
PHP 7.3.6 Development Server started at Sat Jun 22 20:00:28 2019
Listening on http://localhost:8080
Document root is /private/tmp/test
Press Ctrl-C to quit.
[Sat Jun 22 20:00:32 2019] [::1]:51640 [200]: /
[Sat Jun 22 20:00:32 2019] [::1]:51641 [200]: /style.css
[Sat Jun 22 20:02:35 2019] [::1]:51670 [404]: /oops.html - No such file or directory

As you can see, root folder (/, which was translated to index.html in my case) and a stylesheet (style.css) were requested and successfully delivered (code is 200). But non-existent file oops.html resulted in error (code is 404).

like image 35
alx Avatar answered Oct 10 '22 21:10

alx