aaaiiuie

学習(備忘録)のため運用

Node.js http-serverを使う

node.js を使って、最も簡単にwebサーバーを作る方法。

https://github.com/nodeapps/http-server

このhttp-serverをインストールしておくと、任意のディレクトリで http-server というコマンドを実行するだけでそのディレクトリをドキュメントルートにしたウェブサーバーが起動する。

$ http-server 
Starting up http-server, serving ./ on: http://0.0.0.0:8080
Hit CTRL-C to stop the server

インストール

Node.jsが既にインストールされているのであれば以下のコマンドを実行するだけで良い。

$ npm install -g http-server

利用

ドキュメントルートにしたいディレクトリに移動してコマンドを叩くだけ。

$ http-server

これでブラウザを開いてくれる。

$ http-server -o

以下ヘルプ(オプション)

$ http-server -h
usage: http-server [path] [options]

options:
  -p           Port to use [8080]
  -a           Address to use [0.0.0.0]
  -d           Show directory listings [true]
  -i           Display autoIndex [true]
  -g --gzip    Serve gzip files when possible [false]
  -e --ext     Default file extension if none supplied [none]
  -s --silent  Suppress log messages from output
  --cors[=headers]   Enable CORS via the "Access-Control-Allow-Origin" header
                     Optionally provide CORS headers list separated by commas
  -o [path]    Open browser window after starting the server
  -c           Cache time (max-age) in seconds [3600], e.g. -c10 for 10 seconds.
               To disable caching, use -c-1.
  -U --utc     Use UTC time format in log messages.

  -P --proxy   Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com

  -S --ssl     Enable https.
  -C --cert    Path to ssl cert file (default: cert.pem).
  -K --key     Path to ssl key file (default: key.pem).

  -r --robots  Respond to /robots.txt [User-agent: *\nDisallow: /]
  --no-dotfiles  Do not show dotfiles
  -h --help    Print this list and exit.