Set up fast simple http or https server for tests

In case you ever wanted to test simple web page, just html with or even without javascript and for whatever reason it needs to run on web server instead of opening file in the browser, there’s no need to setup big full servers like IIS (Internet Information Services), Apache or Nginx. Sometimes just fast command line tools will make the same and faster to set up. This article will introduce some ways how to set up fast simple Http or https server from command line without IIS or Apache.

Depends on our usual dev stack there are some possibilities what server to run. These days it’s quite common that almost every developer has installed nodejs or at least something similar with own package manager. There are also some alternatives for php or python devs.

1. Http server using Node.js

In case there’s an Node.js with npm installed on the dev machine, then it’s pretty simple how to run http or https server.

  1. Open the terminal or console and install npm package http-server
    • npm i -g http-server
  2. Run server (server will use current folder as web root)
    • http-server (runs server on default 0.0.0.0:8080
    • http-server -p <port> (runs server with custom port number 0.0.0.0:<port>)

1.1 Https server using Node.js

To run https server you will need first a self signed certificate. You can generate one with openssl which is usually part of git installation:

openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem

After answering a few question it will generate certificate which can be used not only with http-server. But now to run https server just run:

http-server -S -C cert.pem

2. Http server using Python 3

When python 3 is installed on the dev machine then run this command line to get http server on:

python -m http.server

3. Http server using PHP

If you need to test not only html and javascript but also php scripts then you can use php processor which is part of php package. To run it just specify port and directory to be served:

.\php.exe -S localhost:8081 -t D:\web\root\to\test

4. VS Code extension: Live Server (Five Server)

If you use Visual Studio Code for development Live Server is nice extension which has also hot-reload and all changes are directly visible on the web page preview.


For more tools which can help during development you can check also free online tools like unix timestamp conversion or url/html/base64 encoder/decoder


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *