node-static は、node のコマンドラインユーティリティです。マシン上の任意のディレクトリで起動することで、ローカルなWebサーバを立ち上げることができます。ソフトウェア開発やテストでは、Webサーバが必要になることが少なくありません。また、HTMLやCSSを使ったデザインの作業でも必要となることがあります。しかし、Webサーバーを用意したり、既存の環境を都度変えることは面倒なことです。node-static は確認作業やテストといった場面で気軽に利用できるWebサーバです。

node-static の主な特徴

1) 起動は簡単

カレントディレクトリで起動した例を以下に示します。

$ static
serving "." at http://127.0.0.1:8080

デフォルトのポート番号は 8080 です。Webブラウザで localhost:8080 にアクセスします。

2) ポート番号はオプションで変更可能

次のようにオプションが用意されており、ポート番号も変更可能です。複数のWebサーバーを異なるディレクトリで立ち上げたい場合に利用します。Cache-Control ヘッダを使ってキャッシュに関する設定を行うこともできます。

$ static -h
USAGE: static [-p <port>] [<directory>]

simple, rfc 2616 compliant file streaming module for node

Options:
  --port, -p          TCP port at which the files will be served                        [default: 8080]
  --host-address, -a  the local network interface at which to listen                    [default: "127.0.0.1"]
  --cache, -c         "Cache-Control" header setting, defaults to 3600                
  --version, -v       node-static version                                             
  --headers, -H       additional headers (in JSON format)                             
  --header-file, -f   JSON file of additional headers                                 
  --gzip, -z          enable compression (tries to serve file of same name plus '.gz')
  --help, -h          display this help message                                       

3) 負荷テストの結果を公表しています。

公表されている結果は次の通りです。

This is ApacheBench, Version 2.3 < $Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Server Software: node-static/0.3.0
Server Hostname: 127.0.0.1
Server Port: 8080
Document Path: /lib/node-static.js
Document Length: 6038 bytes
Concurrency Level: 20
Time taken for tests: 2.323 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 63190000 bytes
HTML transferred: 60380000 bytes
Requests per second: 4304.67 [#/sec] (mean)
Time per request: 4.646 [ms] (mean)
Time per request: 0.232 [ms] (mean, across all concurrent requests)
Transfer rate: 26563.66 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.2 0 3
Processing: 1 4 1.4 4 28
Waiting: 1 4 1.3 4 18
Total: 2 5 1.5 4 28
Percentage of the requests served within a certain time (ms)
50% 4
66% 5
75% 5
80% 5
90% 5
95% 6
98% 8
99% 9
100% 28 (longest request)

node-static のインストール

npm install -g node-static でインストールできます。

ubuntu ではパッケージ・ソフトウェアとして提供しています。node や npm をインストールしていない環境でも、次の方法でインストールできます。コマンド名が node-static に変わる点に注意してください。

sudo apt-get install node-static

ソフトウェア開発において、利用者にUIをじっくりと確認してもらう時に、利用者のPCに簡単に環境を作れます。ソフトウェア開発やテストのみならず、静的なコンテンツで構成した環境はいくつもあるでしょう。Webブラウザを利用してドキュメントや画像を見せて、打合せや報告を行うこともできます。そのような場面で活躍するソフトウェアではないでしょうか。

node-static はMIT Licenseのオープンソース・ソフトウェアです。

cloudhead / node-static

source code