expressのインストール

node.jsのフレームワーク、expressのインストールを行います。 基本的にnpmを叩くだけで入ります。通常のパッケージ管理ソフトと同様にnpmが依存関係にあるインストールされていないライブラリも自動的に取得してくれます。

npm install -g express

expressの本体をインストールします。

$ sudo npm install -g express
npm http GET https://registry.npmjs.org/express
npm http 304 https://registry.npmjs.org/express
npm http GET https://registry.npmjs.org/accepts
npm http GET https://registry.npmjs.org/buffer-crc32/0.2.3
npm http GET https://registry.npmjs.org/debug/1.0.4
npm http GET https://registry.npmjs.org/depd/0.4.4
npm http GET https://registry.npmjs.org/escape-html/1.0.1
npm http GET https://registry.npmjs.org/finalhandler/0.1.0
npm http GET https://registry.npmjs.org/media-typer/0.2.0
npm http GET https://registry.npmjs.org/methods/1.1.0
npm http GET https://registry.npmjs.org/parseurl
npm http GET https://registry.npmjs.org/path-to-regexp/0.1.3
npm http GET https://registry.npmjs.org/proxy-addr/1.0.1
npm http GET https://registry.npmjs.org/qs/1.2.2
npm http GET https://registry.npmjs.org/range-parser/1.0.0
npm http GET https://registry.npmjs.org/send/0.8.3
npm http GET https://registry.npmjs.org/serve-static
npm http GET https://registry.npmjs.org/type-is
npm http GET https://registry.npmjs.org/vary/0.1.0
npm http GET https://registry.npmjs.org/cookie/0.1.2
npm http GET https://registry.npmjs.org/fresh/0.2.2
npm http GET https://registry.npmjs.org/cookie-signature/1.0.4
npm http GET https://registry.npmjs.org/merge-descriptors/0.0.2
npm http GET https://registry.npmjs.org/utils-merge/1.0.0
npm http 200 https://registry.npmjs.org/accepts
npm http 200 https://registry.npmjs.org/parseurl
npm http GET https://registry.npmjs.org/parseurl/-/parseurl-1.3.0.tgz
npm http 200 https://registry.npmjs.org/serve-static
npm http 200 https://registry.npmjs.org/type-is
npm http GET https://registry.npmjs.org/accepts/-/accepts-1.0.7.tgz
npm http GET https://registry.npmjs.org/serve-static/-/serve-static-1.5.3.tgz
npm http GET https://registry.npmjs.org/type-is/-/type-is-1.3.2.tgz
npm http 200 https://registry.npmjs.org/parseurl/-/parseurl-1.3.0.tgz
npm http 200 https://registry.npmjs.org/accepts/-/accepts-1.0.7.tgz
npm http 200 https://registry.npmjs.org/serve-static/-/serve-static-1.5.3.tgz
npm http 200 https://registry.npmjs.org/type-is/-/type-is-1.3.2.tgz
npm http 200 https://registry.npmjs.org/proxy-addr/1.0.1
npm http 200 https://registry.npmjs.org/methods/1.1.0
npm http 200 https://registry.npmjs.org/buffer-crc32/0.2.3
npm http 200 https://registry.npmjs.org/finalhandler/0.1.0
npm http 200 https://registry.npmjs.org/cookie-signature/1.0.4
npm http 200 https://registry.npmjs.org/escape-html/1.0.1
npm http 200 https://registry.npmjs.org/send/0.8.3
npm http GET https://registry.npmjs.org/proxy-addr/-/proxy-addr-1.0.1.tgz
npm http GET https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.3.tgz
npm http GET https://registry.npmjs.org/finalhandler/-/finalhandler-0.1.0.tgz
npm http GET https://registry.npmjs.org/methods/-/methods-1.1.0.tgz
npm http 200 https://registry.npmjs.org/path-to-regexp/0.1.3
npm http GET https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.4.tgz
npm http 200 https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.3.tgz
npm http GET https://registry.npmjs.org/escape-html/-/escape-html-1.0.1.tgz
npm http 200 https://registry.npmjs.org/proxy-addr/-/proxy-addr-1.0.1.tgz
npm http 200 https://registry.npmjs.org/methods/-/methods-1.1.0.tgz
npm http GET https://registry.npmjs.org/send/-/send-0.8.3.tgz
npm http 200 https://registry.npmjs.org/finalhandler/-/finalhandler-0.1.0.tgz
npm http 200 https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.4.tgz
npm http GET https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.3.tgz
npm http 200 https://registry.npmjs.org/escape-html/-/escape-html-1.0.1.tgz
npm http 200 https://registry.npmjs.org/send/-/send-0.8.3.tgz
npm http 200 https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.3.tgz
npm http 200 https://registry.npmjs.org/qs/1.2.2
npm http 200 https://registry.npmjs.org/cookie/0.1.2
npm http GET https://registry.npmjs.org/qs/-/qs-1.2.2.tgz
npm http 200 https://registry.npmjs.org/debug/1.0.4
npm http 200 https://registry.npmjs.org/merge-descriptors/0.0.2
npm http 200 https://registry.npmjs.org/media-typer/0.2.0
npm http 200 https://registry.npmjs.org/utils-merge/1.0.0
npm http GET https://registry.npmjs.org/cookie/-/cookie-0.1.2.tgz
npm http 200 https://registry.npmjs.org/qs/-/qs-1.2.2.tgz
npm http GET https://registry.npmjs.org/debug/-/debug-1.0.4.tgz
npm http GET https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-0.0.2.tgz
npm http GET https://registry.npmjs.org/media-typer/-/media-typer-0.2.0.tgz
npm http GET https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.0.tgz
npm http 200 https://registry.npmjs.org/cookie/-/cookie-0.1.2.tgz
npm http 200 https://registry.npmjs.org/debug/-/debug-1.0.4.tgz
npm http 200 https://registry.npmjs.org/media-typer/-/media-typer-0.2.0.tgz
npm http 200 https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-0.0.2.tgz
npm http 200 https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.0.tgz
npm http 200 https://registry.npmjs.org/depd/0.4.4
npm http GET https://registry.npmjs.org/depd/-/depd-0.4.4.tgz
npm http 200 https://registry.npmjs.org/fresh/0.2.2
npm http 200 https://registry.npmjs.org/vary/0.1.0
npm http 200 https://registry.npmjs.org/range-parser/1.0.0
npm http 200 https://registry.npmjs.org/depd/-/depd-0.4.4.tgz
npm http GET https://registry.npmjs.org/vary/-/vary-0.1.0.tgz
npm http GET https://registry.npmjs.org/fresh/-/fresh-0.2.2.tgz
npm http GET https://registry.npmjs.org/range-parser/-/range-parser-1.0.0.tgz
npm http 200 https://registry.npmjs.org/vary/-/vary-0.1.0.tgz
npm http 200 https://registry.npmjs.org/fresh/-/fresh-0.2.2.tgz
npm http 200 https://registry.npmjs.org/range-parser/-/range-parser-1.0.0.tgz
npm http GET https://registry.npmjs.org/mime-types
npm http GET https://registry.npmjs.org/negotiator/0.4.7
npm http GET https://registry.npmjs.org/mime-types
npm http GET https://registry.npmjs.org/ipaddr.js/0.1.2
npm http 200 https://registry.npmjs.org/mime-types
npm http 200 https://registry.npmjs.org/mime-types
npm http GET https://registry.npmjs.org/mime-types/-/mime-types-1.0.2.tgz
npm http GET https://registry.npmjs.org/destroy/1.0.3
npm http GET https://registry.npmjs.org/mime/1.2.11
npm http GET https://registry.npmjs.org/ms/0.6.2
npm http GET https://registry.npmjs.org/on-finished/2.1.0
npm http 200 https://registry.npmjs.org/mime-types/-/mime-types-1.0.2.tgz
npm http 200 https://registry.npmjs.org/ms/0.6.2
npm http GET https://registry.npmjs.org/ms/-/ms-0.6.2.tgz
npm http 200 https://registry.npmjs.org/ms/-/ms-0.6.2.tgz
npm http 200 https://registry.npmjs.org/ipaddr.js/0.1.2
npm http GET https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-0.1.2.tgz
npm http 200 https://registry.npmjs.org/negotiator/0.4.7
npm http GET https://registry.npmjs.org/negotiator/-/negotiator-0.4.7.tgz
npm http 200 https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-0.1.2.tgz
npm http 200 https://registry.npmjs.org/on-finished/2.1.0
npm http GET https://registry.npmjs.org/on-finished/-/on-finished-2.1.0.tgz
npm http 200 https://registry.npmjs.org/negotiator/-/negotiator-0.4.7.tgz
npm http 200 https://registry.npmjs.org/on-finished/-/on-finished-2.1.0.tgz
npm http 200 https://registry.npmjs.org/destroy/1.0.3
npm http GET https://registry.npmjs.org/destroy/-/destroy-1.0.3.tgz
npm http 200 https://registry.npmjs.org/destroy/-/destroy-1.0.3.tgz
npm http 200 https://registry.npmjs.org/mime/1.2.11
npm http GET https://registry.npmjs.org/mime/-/mime-1.2.11.tgz
npm http 200 https://registry.npmjs.org/mime/-/mime-1.2.11.tgz
npm http GET https://registry.npmjs.org/ee-first/1.0.5
npm http 200 https://registry.npmjs.org/ee-first/1.0.5
npm http GET https://registry.npmjs.org/ee-first/-/ee-first-1.0.5.tgz
npm http 200 https://registry.npmjs.org/ee-first/-/ee-first-1.0.5.tgz
express@4.8.5 /usr/local/lib/node_modules/express
├── merge-descriptors@0.0.2
├── utils-merge@1.0.0
├── cookie@0.1.2
├── escape-html@1.0.1
├── cookie-signature@1.0.4
├── finalhandler@0.1.0
├── fresh@0.2.2
├── vary@0.1.0
├── range-parser@1.0.0
├── media-typer@0.2.0
├── parseurl@1.3.0
├── methods@1.1.0
├── serve-static@1.5.3
├── buffer-crc32@0.2.3
├── path-to-regexp@0.1.3
├── depd@0.4.4
├── qs@1.2.2
├── debug@1.0.4 (ms@0.6.2)
├── type-is@1.3.2 (mime-types@1.0.2)
├── proxy-addr@1.0.1 (ipaddr.js@0.1.2)
├── accepts@1.0.7 (negotiator@0.4.7, mime-types@1.0.2)
└── send@0.8.3 (destroy@1.0.3, ms@0.6.2, mime@1.2.11, on-finished@2.1.0)

npm install -g express-generator

expressコマンドが使えるようにします。 expressコマンドはアプリケーションのひな形を自動生成してくれる物。

$ sudo npm install -g express-generator
Password:
npm http GET https://registry.npmjs.org/express-generator
npm http 200 https://registry.npmjs.org/express-generator
npm http GET https://registry.npmjs.org/express-generator/-/express-generator-4.2.0.tgz
npm http 200 https://registry.npmjs.org/express-generator/-/express-generator-4.2.0.tgz
npm http GET https://registry.npmjs.org/commander/1.3.2
npm http GET https://registry.npmjs.org/mkdirp/0.3.5
npm http 200 https://registry.npmjs.org/mkdirp/0.3.5
npm http GET https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.5.tgz
npm http 200 https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.5.tgz
npm http 200 https://registry.npmjs.org/commander/1.3.2
npm http GET https://registry.npmjs.org/commander/-/commander-1.3.2.tgz
npm http 200 https://registry.npmjs.org/commander/-/commander-1.3.2.tgz
npm http GET https://registry.npmjs.org/keypress
npm http 200 https://registry.npmjs.org/keypress
npm http GET https://registry.npmjs.org/keypress/-/keypress-0.1.0.tgz
npm http 200 https://registry.npmjs.org/keypress/-/keypress-0.1.0.tgz
/usr/local/bin/express -> /usr/local/lib/node_modules/express-generator/bin/express
express-generator@4.2.0 /usr/local/lib/node_modules/express-generator
├── mkdirp@0.3.5
└── commander@1.3.2 (keypress@0.1.0)

アプリの作成テスト

expressコマンドにアプリ名を指定するだけでサクッとひな形を作成してくれます。

$ express test

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

   install dependencies:
     $ cd test && npm install

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

依存関係にあるライブラリをインストールします。 expressが生成したpackage.jsonに必要とするライブラリ一覧が書かれているのですが、npmコマンドがそれを見て必要な物を自動で入れてくれます。

$ cd test
$ npm install -d

nodeを起動して様子を見ます。 特にエラーなどでなければOK。

$ node app.js

ブラウザからの確認

とりあえずサクッと動作確認がしてしまいたい場合、app.jsの下の方に以下のようなコードを付け足して、

app.listen(3000);

node.jsを起動し、ブラウザから localhost:3000 へアクセスすると簡単なページが表示されるようになります。アクセスがあるとそのログがターミナル上にも表示され確認できます。

$ node app.js
GET / 200 321ms - 170b
GET /stylesheets/style.css 200 22ms - 110b

終了するときはCtrl+cあたりで適当に。

関連書籍

Bear Bibeault, John Resig

松島 浩道
ISBN 4797370904
¥ 3,132

failed to fetch data: unkown error

高橋 登史朗
ISBN 4798037060
¥ 21,777

David Flanagan
ISBN 4873115736
¥ 4,536

コメント

コメントを入力. Wiki文法が有効です:
V M G F D