LoginSignup
12
11

More than 5 years have passed since last update.

Express4.xでプロジェクト作成

Last updated at Posted at 2014-07-01

環境:Mac OS X 10.9.4

Expressは、Node.jsのWebアプリケーションフレームワーク。

まず、Expressをグローバルインストールする。

$ npm install -g express
npm http GET https://registry.npmjs.org/express
:
:
express@4.4.5 /Users/username/.nvm/v0.11.13/lib/node_modules/express
├── parseurl@1.0.1
├── utils-merge@1.0.0
├── merge-descriptors@0.0.2
├── cookie@0.1.2
├── escape-html@1.0.1
├── cookie-signature@1.0.4
├── range-parser@1.0.0
├── fresh@0.2.2
├── vary@0.1.0
├── qs@0.6.6
├── methods@1.0.1
├── serve-static@1.2.3
├── buffer-crc32@0.2.3
├── path-to-regexp@0.1.2
├── debug@1.0.2 (ms@0.6.2)
├── proxy-addr@1.0.1 (ipaddr.js@0.1.2)
├── accepts@1.0.6 (negotiator@0.4.7, mime-types@1.0.1)
├── type-is@1.2.1 (mime-types@1.0.0)
└── send@0.4.3 (mime@1.2.11, finished@1.2.2)

インストールしたExpressのバージョンを確認する。

express -V
-bash: express: command not found

あれ!? expressコマンドが動かない。昔はこれでバージョン表示されたのに…
どうやら、Express3.xではプロジェクトの雛形を用意するexpressコマンドが使えたが、Express4.xでは廃止になって、別パッケージとして分離された様です。

と、言うことでexpress-generatorをインストールする。

$ npm install -g express-generator
npm http GET https://registry.npmjs.org/express-generator
:
:
express-generator/bin/express
express-generator@4.2.0 /Users/username/.nvm/v0.11.13/lib/node_modules/express-generator
├── mkdirp@0.3.5
└── commander@1.3.2 (keypress@0.1.0)

これでexpressコマンドが使える様になります。

$ express -V
4.2.0

expressプロジェクトを作成する。今回はsampleプロジェクトを作成。

$ express sample

   create : sample
   create : sample/package.json
   create : sample/app.js
   create : sample/public
   create : sample/public/javascripts
   create : sample/routes
   create : sample/routes/index.js
   create : sample/routes/users.js
   create : sample/public/images
   create : sample/public/stylesheets
   create : sample/public/stylesheets/style.css
   create : sample/views
   create : sample/views/index.jade
   create : sample/views/layout.jade
   create : sample/views/error.jade
   create : sample/bin
   create : sample/bin/www

   install dependencies:
     $ cd sample && npm install

   run the app:
     $ DEBUG=sample ./bin/www

sampleディレクトリが作成されます。作成されたプロジェクトディレクトリの中に、必要なexpressモジュールを含んだpackage.jsonが作成されているので、次のコマンドを入力してライブラリをインストールする。

$ cd sample
$ npm install

作成したプロジェクトを実行する。

$ node app.js
$

あれ!?HTTPサーバーが実行された気配がないぞ?
どうやら、場所とファイル名が変わっているみたい。(sample/bin/wwwに変わっている)

$ DEBUG=sample ./bin/www

or

$ node bin/www

これでHTTPサーバーが起動するので、ブラウザで、http://localhost:3000 を入力する。
「Welcome to Express」が表示されます。

Welcome to Express

動いているサーバーを止めるには「control+c」をを押します。

12
11
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
12
11