http-server README

The project's README from github.com/http-party/http-server. As one markdown file: README.md.

GitHub Workflow Status (master) npm homebrew npm downloads license

http-server: a simple static HTTP server

http-server is a simple, zero-configuration command-line static HTTP server. It is powerful enough for production usage, but it's simple and hackable enough to be used for testing, local development and learning.

Example of running http-server

Installation:

Running on-demand:

Using npx you can run the script without installing it first:

npx http-server [path] [options]

Globally via npm

npm install --global http-server

This will install http-server globally so that it may be run from the command line anywhere.

Globally via Homebrew

brew install http-server
 

As a dependency in your npm package:

npm install http-server

Using Docker

Note: a public image is not provided currently, but you can build one yourself with the provided Dockerfile.

  1. Create an image
    docker build -t my-image .
    
  2. Run a container
    docker run -p 8080:8080 -v "${pwd}:/public" my-image
    
    In the example above we're serving the directory ./ (working directory). If you wanted to serve ./test you'd replace ${pwd} with ${pwd}/test.

Usage:

 http-server [path] [options]

[path] defaults to ./public if the folder exists, and ./ otherwise.

Now you can visit http://localhost:8080 to view your server

Note: Caching is on by default. Add -c-1 as an option to disable caching.

Available Options:

Command Description Defaults
-p or --port Port to use. Use -p 0 to look for an open port, starting at 8080. It will also read from process.env.PORT. 8080
-a Address to use 0.0.0.0
--base-dir Base path to serve files from /
-d Show directory listings true
-dir-overrides-404 Whether -d should override magic 404.html false
-i Display autoIndex true
-g or --gzip When enabled it will serve ./public/some-file.js.gz in place of ./public/some-file.js when a gzipped version of the file exists and the request accepts gzip encoding. If brotli is also enabled, it will try to serve brotli first. false
-b or --brotli When enabled it will serve ./public/some-file.js.br in place of ./public/some-file.js when a brotli compressed version of the file exists and the request accepts br encoding. If gzip is also enabled, it will try to serve brotli first. false
-e or --ext Default file extension if none supplied html
-s or --silent Suppress log messages from output
--coop Enable COOP via the Cross-Origin-Opener-Policy header
--cors Enable CORS via the Access-Control-Allow-Origin header
--private-network-access Enable Private Network Access via the Access-Control-Allow-Private-Network header
--cors Enable CORS via the Access-Control-Allow-Origin: * header. Optionally provide comma-separated values to add to Access-Control-Allow-Headers
-H or --header Add an extra response header (can be used several times)
-o [path] Open browser window after starting the server. Optionally provide a URL path to open. e.g.: -o /other/dir/
-c Set cache time (in seconds) for cache-control max-age header, e.g. -c10 for 10 seconds. To disable caching, use -c-1. 3600
-t Connection timeout in seconds, e.g. -t60 for 1 minute. To disable timeout, use -t0. 120
-T or --title Custom title suffix for the terminal window. The title will be "http-server PORT [TITLE]".
-U or --utc Use UTC time format in log messages.
--log-ip Enable logging of the client's IP address false
-P or --proxy Proxies all requests which can't be resolved locally to the given url. e.g.: -P http://someurl.com
--proxy-options Pass proxy options using nested dotted objects. e.g.: --proxy-options.secure false
--proxy-config Pass in .json configuration file or stringified JSON. e.g.: ./path/to/config.json
--proxy-all Forward every request to the proxy target instead of serving local files false
--proxy-options Pass proxy options using nested dotted objects. e.g.: --proxy-options.secure false
--user or --username Username for basic authentication
--password Password for basic authentication
-S, --tls or --ssl Enable secure request serving with TLS/SSL (HTTPS) false
-C or --cert Path to ssl cert file cert.pem
-K or --key Path to ssl key file key.pem
-r or --robots Automatically provide a /robots.txt (The content of which defaults to User-agent: *\nDisallow: /) false
--no-dotfiles Do not show dotfiles
--mimetypes Path to a .types file for custom mimetype definition
--hide-permissions Do not show file permissions
--allowed-hosts Comma-separated list of hosts allowed to access the server. e.g.: --allowed-hosts localhost,example.com
-h or --help Print this list and exit.
-v or --version Print the version and exit.
--no-panic Don't print error stack in the console, put it in a log file false

Magic Files

Catch-all redirect

To implement a catch-all redirect, use the index page itself as the proxy with:

http-server --proxy http://localhost:8080?

Note the ? at the end of the proxy URL. Thanks to @houston3 for this clever hack!

TLS/SSL

First, you need to make sure that openssl is installed correctly, and you have key.pem and cert.pem files. You can generate them using this command:

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

You will be prompted with a few questions after entering the command. Use 127.0.0.1 as value for Common name if you want to be able to install the certificate in your OS's root certificate store or browser so that it is trusted.

This generates a cert-key pair and it will be valid for 3650 days (about 10 years).

Then you need to run the server with -S for enabling SSL and -C for your certificate file.

http-server -S -C cert.pem

If you wish to use a passphrase with your private key you can include one in the openssl command via the -passout parameter (using password of foobar)

e.g. openssl req -newkey rsa:2048 -passout pass:foobar -keyout key.pem -x509 -days 365 -out cert.pem

For security reasons, the passphrase will only be read from the NODE_HTTP_SERVER_SSL_PASSPHRASE environment variable.

This is what should be output if successful:

Starting up http-server, serving ./ through https

http-server settings:
COOP: disabled
CORS: disabled
Cache: 3600 seconds
Connection Timeout: 120 seconds
Directory Listings: visible
AutoIndex: visible
Serve GZIP Files: false
Serve Brotli Files: false
Default File Extension: none

Available on:
  https://127.0.0.1:8080
  https://192.168.1.101:8080
  https://192.168.1.104:8080
Hit CTRL-C to stop the server

Development

Checkout this repository locally, then:

$ npm i
$ npm start

Now you can visit http://localhost:8080 to view your server

You should see the turtle image in the screenshot above hosted at that URL. See the ./public folder for demo content.

http-server (http-party/http-server)

Searching Database…

这是什么东西?http-server 是一个简单、零配置的命令行静态 HTTP 服务器:把它指向一个文件夹,它就能通过 HTTP 提供里面的文件。README 说它强大到可以用于生产环境,同时又足够简单、易于修改,适合用来做测试、本地开发和学习。
它是命令行工具还是一个库?两者都是。它主要是一个命令行工具——在任意文件夹里运行 http-server [path] [options] 即可——但你也可以把它作为依赖装进自己的 npm 包里,在代码中调用。
我怎么把它跑起来?只是临时用一下的话,直接运行 npx http-server [path] [options],完全不需要安装。想长期保留的话,用 npm install --global http-server 全局安装,macOS 上也可以 brew install http-server,然后在你想要提供服务的文件夹里运行 http-server 就行。
我需要事先装好哪些东西?只需要一个 Node.js 运行环境和 npm——这个包要求 Node >=16.20.2。在 macOS 上可以完全不用 npm,改用 Homebrew 安装。不需要数据库或其他任何服务。
为什么我刷新页面时老是看到旧文件?缓存默认是开启的,cache-control 的 max-age 设为 3600 秒。开发的时候加上 -c-1 来禁用缓存,这样每次请求都会拿到你最新的改动。
它在 Windows 和 macOS 上能用吗,还是只能在 Linux 上跑?只要 Node 能运行的地方它都能跑——Windows、macOS 和 Linux 都行——因为它就是一个纯 Node.js 命令行工具,README 里没有任何针对特定操作系统的要求。项目里附带了一个 Dockerfile,可以用来构建你自己的镜像,因为它没有发布公开的 Docker 镜像。
我能用它通过 HTTPS 对外提供一个真正的网站吗?可以——README 说它强大到足以用于生产环境。启动时加上 -S(或 --tls/--ssl),再配合 -C cert.pem 和 -K key.pem 就能启用 HTTPS;它还支持通过 --username/--password 做基础认证、通过 --cors 启用 CORS,以及自定义响应头。
它支持哪些特殊文件?访问目录时默认会返回 index.html,找不到文件时会返回自定义的 404.html——README 建议用这个特性来做单页应用(SPA)托管,让它返回入口页面。
它能当代理转发到后端吗?可以:-P 或 --proxy 会把本地无法处理的请求转发到指定的 URL,而 --proxy-all 则把所有请求都转发到代理目标,不再提供本地文件。
它会压缩响应内容吗?只会使用预先压缩好的文件:加上 -g 或 --gzip 后,如果磁盘上存在 .gz 版本且请求接受 gzip,就会用它代替原文件返回;-b 或 --brotli 对 .br 文件做同样的事,两者都开启时优先使用 brotli。
不带任何参数运行时,我默认能得到什么?它会监听 8080 端口,显示目录列表和自动索引,把 cache-control 设为一小时,并把 index.html 作为默认文档;每个默认值都可以用 -p、-d、-c 这类参数来修改。
What are similar projects?hotel, dufs, qrcp, caddy, truffle, portless, readme-md-generator, razzle
Creator
http-party
Size
2.2 MB
Stars
14.2K
Age
15y
Commits · 90 Days