Sometimes when prototyping I just need a HTML file and a little bit of CSS. Viewing this with a local server is a lot more true to form than opening the file in a browser. Here's how to do it in a few different languages, depending on what you have installed.
For the sake of brevity, I'm going to assume you have a file called index.html
in your current directory, and you're running the commands from that directory.
Python
Good ol' Python makes it easy and built-in. Just run the following command:
python3 -m http.server
This will start a server on port 8000
, and to change that port just pass it like so: python3 -m http.server 8001
.
Python is probably a lot more commonly installed than other options, so this is a good first one to try.
PHP
Hat tip to PHP here for keeping it simple:
php -S localhost:8000
The syntax here should look familiar, but PHP doesn't assume anything with your port, so don't forget to pass one.
Rust
Rust doesn't ship with a built-in server, but it makes it easy to get one. I've used cargo-server
project link, which you'll need to install first with cargo
.
cargo install cargo-server
This isn't instant, but it's pretty quick. Once it's done, you can run the following command to get a server on port 8000
:
cargo server
Change the port by passing --port <port>
.
Node
Lastly, I use http-server
project link, which uses npx
. You can install http-server
globally, but I prefer to use npx
to avoid global installs.
npx http-server
http-server
takes a few options, but the only one I find myself using is -p
to specify the port. By default, it uses 8080
.