Yesterday, I posted a code sample of the simplest web server I could create using node.js. After reading my post, Tugberk Ugurlut pointed me to http-server and his blog post on http-server.
Install http-server globally
Installing http-server is simple with the following command:
install http-server -g
The “-g” switch installs this library locally. I tried it without that switch, but it did not work. However, the advantage of installing it globally is that it will work in any new folder you create on your computer.
Create Web Files to Render
You can change to a folder on your local machine and create some web pages, JavaScript files, and CSS files, such as the 2 sample pages below:
index.html
<html>
<body>
<h1>Indexh1>
<div>
Welcome to my site!
div>
body>
html>
about.html
<html>
<body>
<h1>Abouth1>
<div>
This is a site!
div>
body>
html>
Start Web Server
http-server
Test it Out
Now you can open a browser and navigate to a page, as shown below:
Notice that you don’t need to type the “.html” or “.htm” extension. The server is smart enough to know the page you are looking for.
It is also smart enough to assume a page with a name like “index.html”, “home.htm”, or “default.htm” is a a default page and render this page when none is specified, as shown below:
This is a very simple way to quickly install, configure, and start a lightweight web server on your local PC for testing or creating static pages.