46
42

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

npmのコマンドでローカルサーバーを簡単に立ち上げる

Last updated at Posted at 2016-01-22

phpの簡易サーバーのコマンドを、package.jsonscriptsに設定しておくとHTMLとかPHPをサーバーにアップしなくても動作確認できます。

phpファイルも動作するので、ちょっとした開発ツールを用意したりもできると思います。

ファイルを置いてコマンドを1つ覚えるだけ

package.jsonの内容

[package.json]
{
  "name": "Project name",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "server": "php -S localhost:8000 -t ./htdocs"
  },
}

ドキュメントルートは./htdocsのところを適宜変えてください。

ディレクトリ構成

/project_directory/
  htdocs/
    img/
    js/
    css/
    index.php
  package.json

起動方法

ターミナルで1行入れるだけです。

$ cd project_directory #package.jsonのある場所に移動しておく
$ npm run server

停止方法

Cmd(Ctrl) + C

Python版もあるよ

"scripts" : { "server" : "cd ../public && python -m SimpleHTTPServer" },
46
42
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
46
42

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?