Suppose you are in folder
xyz, and wanted to start a server there so that you can see the files in browser. Or you have created a new build of the project and wanted to know if the files render properly inside
There are many cases where you just want to go to some folder and render the contents in browser using http server.
http-server is a npm package which allows you to serve the contents of folder by running a http server. In order to use it, install it globally.
npm install http-server --save
Now you have
http-server available globally in your machine.
The process of starting http server is simple. Just go the folder where you want to run the server, and type the following command
By default it will run in port 8080. You can specify port by using
http-server -p 8090
The above command will run the server at port 8090
http-serverStarts server at port 8080
http-server -p 8090Starts server at 8090
http-server -p 8090 -gStarts server at 8090 and compresses the content using gzip
http-server -p 8090 -ssl --cert <PATH TO CERT> --key <PATH TO KEY>Starts server at 8090 and uses ssl certificate. Scroll below to learn how to generate test certificate and key on your computer
Certificate is the public key while
key refers to the private key. In order to generate a test certificate and key on your machine, you have to use
openssl. Run the following commands to generate the certificate.
openssl genrsa 2048 > private.key
This creates the private key file with the name
host.key. Now we will have to change the permissions for this file, as this file cannot be open to public. So change the permission to 400.
chmod 400 private.key
Now that we have a private key with us, you can generate a public key
openssl req -new -x509 -nodes -sha256 -days 365 -key private.key -out public.cert
So we have a file called
public.cert and a private key file called
private.key. You can use this in http command as follows
http-server -p 8090 -ssl --cert ./public.cert --key ./private.key
You can browse the site at
https://localhost:8090. You will get warning on chrome, which you can accept and bypass it.