Use npm http-server to quickly create a secure web site with host name on Windows

Node and npm makes it really simple to install command line tools. One of my favourites is http-server. Http-server allows you to start a web server from any folder using the command prompt. To do this first install node from https://nodejs.org/en/ this will also install npm. Then open a command prompt and type:

npm install http-server -g

The -g will install http-server globally meaning you can run it from anywhere. You can now point http-server at any folder and it will start a web server with the following command.

http-server

This is cool by itself but there are times when you need go a bit further. For example I might want a site to be served over https and give it a host name like bigbank.com to test something quickly. To do that you need to add the following line to the hosts file in C:\Windows\System32\drivers\etc

127.0.0.1 bigbank.com

Then you can use http-server with the following commands to access https://bigbank.com in your browser:

http-server -a bigbank.com -p 443 -S -K "C:\certs\key.pem" -C "C:\certs\cert.pem"

If you are on a Windows machine you may not have your certs as pem files. This post shows you how to create a self signed cert.

comments powered by Disqus