For the purpose of providing a web service to a Node.js project to users, we may require a small middleware to start an HTTP service to handle the HTTP requests.

Solution: Python

In python, we can use http.server to start a web service.

python3 -m http.server

This command will return the corresponding files based on your requests.

Unfortunately, this does not work for a Node.js project. We may visit http://example.com/foo, but the real target is /index.html. This will raise an unexpected 404 error.

Solution: Nginx

We can also configure Nginx and could get a perfect result.

The configuration file is pretty easy

server {
    listen       80;
    server_name  example.com;

    root /path/to/the/project;
    index index.html;

    location / {
        try_files $uri $uri/ /index.html  =404;
    }
}

However, the steps are kind of too much.

Solution: Hou

Recently, I prepared a small script based on golang. It will do the similar job as the behavior above in Nginx, but much smaller and efficacy.

Install or update from source

If you just wish to use it. Please just use the following command:

go get -v github.com/yuikns/hou

If you wish to make contributions to the repo. You can use the following script.

curl -L https://bit.ly/2KzJeU6 | bash

Docker image

If you are using Chinese network, you may use aliyun's mirror: registry.cn-zhangjiakou.aliyuncs.com/yuikns/hou

Otherwise, you may use yuikns/hou from dockerhub.

Since the logic is very easy and the dependencies on very few packages, this image is only 6MB in the current.

Example of using Docker through docker-compose

A docker-compose.yml file is a YAML file that defines how Docker containers should behave in production.

Here is an example to use Hou easily

version: '3'

services:
  hou:
    image: yuikns/hou:latest
    ports:
      - 6789:6789
    restart: always
    volumes:
      # /app is the default root of the service
      - ./dist:/app 
#    command: ["-d", "-v" ] # other options

Benchmark

Here is a simple benchmark between hou and nginx.

Categories: Code

Yu

Ideals are like the stars: we never reach them, but like the mariners of the sea, we chart our course by them.

Leave a Reply

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