# Monday, May 9, 2016

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>Index</h1>
    <div>
        Welcome to my site!
    </div>
</body>
</html>

about.html

<html>
<body>
    <h1>About</h1>
    <div>
        This is a site!
    </div>
</body>
</html>

Start Web Server

Then, start a web server with the following command:
http-server
 
The server will notify you on what port it is running (8080 by default)

Test it Out

Now you can open a browser and navigate to a page, as shown below:

http-server-about

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:

http-server-index

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.

Monday, May 9, 2016 3:02:55 AM (GMT Daylight Time, UTC+01:00)
Comments are closed.