By HTML Terminal Guru
If you’re a developer, chances are you spend a lot of time in the terminal, and you might be wondering if it’s possible to run your HTML code directly from the terminal. In this blog post, we will learn how to do just that!
In order to follow along with this tutorial, you will need to have the following installed on your machine:
- Node.js – You can download and install the latest version from the official Node.js website.
- A text editor – Any text editor like Visual Studio Code, Sublime Text, or Atom will do.
- A terminal – Your preferred terminal app like Terminal on macOS, Command Prompt or PowerShell on Windows, or any Linux terminal emulator.
Step 1: Install the “http-server” package
In order to run HTML code in your terminal, you’ll need to install a simple HTTP server. We will be using the http-server package for this. To install it globally, open your terminal and run the following command:
npm install -g http-server
Step 2: Create an HTML file
Next, you’ll need an HTML file that you want to run in the terminal. For this tutorial, we’ll create a simple HTML file called index.html with the following content:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Hello, World!</title> </head> <body> <h1>Hello, World!</h1> <p>This is a simple HTML file.</p> </body> </html>
Save this file in a folder on your computer where you want to run the server.
Step 3: Run the HTML file in your terminal
Now that you have your HTML file and the http-server package installed, you can run your HTML code in the terminal. Open your terminal and navigate to the folder where you saved your index.html file. Then, run the following command:
This will start the server on a default port, which is usually 8080. You should see output similar to this:
Starting up http-server, serving ./ Available on: http://127.0.0.1:8080 http://192.168.1.xx:8080 Hit CTRL-C to stop the server
Now, open your preferred web browser and visit http://localhost:8080. You should see the content of your index.html file displayed.
In this blog post, we learned how to run HTML code in the terminal using the http-server package. This method can be useful for quickly testing your HTML files without the need for a full-fledged web server. Happy coding!