2014年7月12日土曜日

express のインストール

前回は「node.jsのインストール」を行いました(゚д゚)(。_。)(゚д゚)(。_。) ウンウン

今回はnode.jsを使って開発を行うのですが、その際に便利な「Express」をインストールしたいと思います。

Expressとは
node.jsように用意されたWebアプリケーションフレームワークのこと。
(テンプレートの機能やURLのルーティングの処理などWebアプリケーションで必要となるものが用意されているらしい)

ということなので早速インストール+(0゚・∀・) + ワクテカ +

express のインストール
# npm install -g express
npm http GET https://registry.npmjs.org/express
npm http 200 https://registry.npmjs.org/express
npm http GET https://registry.npmjs.org/express/-/express-4.6.0.tgz
npm http 200 https://registry.npmjs.org/express/-/express-4.6.0.tgz
npm http GET https://registry.npmjs.org/debug
npm http GET https://registry.npmjs.org/depd
npm http GET https://registry.npmjs.org/escape-html
npm http GET https://registry.npmjs.org/media-typer
npm http GET https://registry.npmjs.org/finalhandler
npm http GET https://registry.npmjs.org/methods
npm http GET https://registry.npmjs.org/parseurl
npm http GET https://registry.npmjs.org/path-to-regexp
npm http GET https://registry.npmjs.org/proxy-addr
npm http GET https://registry.npmjs.org/range-parser
npm http GET https://registry.npmjs.org/send
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
npm http GET https://registry.npmjs.org/cookie
npm http GET https://registry.npmjs.org/fresh
npm http GET https://registry.npmjs.org/cookie-signature
npm http GET https://registry.npmjs.org/merge-descriptors
npm http GET https://registry.npmjs.org/qs
npm http GET https://registry.npmjs.org/utils-merge
npm http GET https://registry.npmjs.org/accepts
npm http GET https://registry.npmjs.org/buffer-crc32
npm http 200 https://registry.npmjs.org/finalhandler
npm http GET https://registry.npmjs.org/finalhandler/-/finalhandler-0.0.3.tgz
npm http 200 https://registry.npmjs.org/escape-html
npm http GET https://registry.npmjs.org/escape-html/-/escape-html-1.0.1.tgz
npm http 200 https://registry.npmjs.org/media-typer
npm http GET https://registry.npmjs.org/media-typer/-/media-typer-0.2.0.tgz
npm http 200 https://registry.npmjs.org/depd
npm http 200 https://registry.npmjs.org/methods
npm http GET https://registry.npmjs.org/depd/-/depd-0.3.0.tgz
npm http GET https://registry.npmjs.org/methods/-/methods-1.1.0.tgz
npm http 200 https://registry.npmjs.org/parseurl
npm http 200 https://registry.npmjs.org/range-parser
npm http 200 https://registry.npmjs.org/debug
npm http 200 https://registry.npmjs.org/proxy-addr
npm http GET https://registry.npmjs.org/range-parser/-/range-parser-1.0.0.tgz
npm http GET https://registry.npmjs.org/parseurl/-/parseurl-1.1.3.tgz
npm http GET https://registry.npmjs.org/debug/-/debug-1.0.3.tgz
npm http GET https://registry.npmjs.org/proxy-addr/-/proxy-addr-1.0.1.tgz
npm http 200 https://registry.npmjs.org/path-to-regexp
npm http 200 https://registry.npmjs.org/vary
npm http GET https://registry.npmjs.org/vary/-/vary-0.1.0.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/serve-static
npm http 200 https://registry.npmjs.org/type-is
npm http 200 https://registry.npmjs.org/escape-html/-/escape-html-1.0.1.tgz
npm http 200 https://registry.npmjs.org/cookie
npm http 200 https://registry.npmjs.org/fresh
npm http 200 https://registry.npmjs.org/cookie-signature
npm http 200 https://registry.npmjs.org/finalhandler/-/finalhandler-0.0.3.tgz
npm http 200 https://registry.npmjs.org/merge-descriptors
npm http 200 https://registry.npmjs.org/range-parser/-/range-parser-1.0.0.tgz
npm http 200 https://registry.npmjs.org/send
npm http 200 https://registry.npmjs.org/media-typer/-/media-typer-0.2.0.tgz
npm http 200 https://registry.npmjs.org/methods/-/methods-1.1.0.tgz
npm http 200 https://registry.npmjs.org/utils-merge
npm http GET https://registry.npmjs.org/serve-static/-/serve-static-1.3.2.tgz
npm http 200 https://registry.npmjs.org/buffer-crc32
npm http GET https://registry.npmjs.org/cookie/-/cookie-0.1.2.tgz
npm http 200 https://registry.npmjs.org/parseurl/-/parseurl-1.1.3.tgz
npm http GET https://registry.npmjs.org/type-is/-/type-is-1.3.2.tgz
npm http GET https://registry.npmjs.org/fresh/-/fresh-0.2.2.tgz
npm http 200 https://registry.npmjs.org/proxy-addr/-/proxy-addr-1.0.1.tgz
npm http 200 https://registry.npmjs.org/debug/-/debug-1.0.3.tgz
npm http GET https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.4.tgz
npm http 200 https://registry.npmjs.org/vary/-/vary-0.1.0.tgz
npm http 200 https://registry.npmjs.org/depd/-/depd-0.3.0.tgz
npm http 200 https://registry.npmjs.org/qs
npm http 200 https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.3.tgz
npm http 200 https://registry.npmjs.org/serve-static/-/serve-static-1.3.2.tgz
npm http GET https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-0.0.2.tgz
npm http 200 https://registry.npmjs.org/cookie/-/cookie-0.1.2.tgz
npm http GET https://registry.npmjs.org/send/-/send-0.6.0.tgz
npm http 200 https://registry.npmjs.org/accepts
npm http 200 https://registry.npmjs.org/type-is/-/type-is-1.3.2.tgz
npm http GET https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.0.tgz
npm http GET https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.3.tgz
npm http 200 https://registry.npmjs.org/fresh/-/fresh-0.2.2.tgz
npm http 200 https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.4.tgz
npm http GET https://registry.npmjs.org/qs/-/qs-0.6.6.tgz
npm http 200 https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.3.tgz
npm http 200 https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-0.0.2.tgz
npm http 200 https://registry.npmjs.org/send/-/send-0.6.0.tgz
npm http 200 https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.0.tgz
npm http GET https://registry.npmjs.org/accepts/-/accepts-1.0.7.tgz
npm http 200 https://registry.npmjs.org/qs/-/qs-0.6.6.tgz
npm http 200 https://registry.npmjs.org/accepts/-/accepts-1.0.7.tgz
npm http GET https://registry.npmjs.org/mime-types
npm http GET https://registry.npmjs.org/negotiator
npm http GET https://registry.npmjs.org/mime-types
npm http GET https://registry.npmjs.org/ipaddr.js
npm http GET https://registry.npmjs.org/finished
npm http GET https://registry.npmjs.org/mime
npm http GET https://registry.npmjs.org/ms
npm http 200 https://registry.npmjs.org/mime-types
npm http GET https://registry.npmjs.org/mime-types/-/mime-types-1.0.1.tgz
npm http 200 https://registry.npmjs.org/mime-types
npm http 200 https://registry.npmjs.org/negotiator
npm http 200 https://registry.npmjs.org/ipaddr.js
npm http 200 https://registry.npmjs.org/mime
npm http 200 https://registry.npmjs.org/ms
npm http GET https://registry.npmjs.org/negotiator/-/negotiator-0.4.7.tgz
npm http GET https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-0.1.2.tgz
npm http GET https://registry.npmjs.org/mime/-/mime-1.2.11.tgz
npm http GET https://registry.npmjs.org/ms/-/ms-0.6.2.tgz
npm http 200 https://registry.npmjs.org/finished
npm http GET https://registry.npmjs.org/finished/-/finished-1.2.2.tgz
npm http 200 https://registry.npmjs.org/mime-types/-/mime-types-1.0.1.tgz
npm http 200 https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-0.1.2.tgz
npm http 200 https://registry.npmjs.org/finished/-/finished-1.2.2.tgz
npm http 200 https://registry.npmjs.org/negotiator/-/negotiator-0.4.7.tgz
npm http 200 https://registry.npmjs.org/ms/-/ms-0.6.2.tgz
npm http 200 https://registry.npmjs.org/mime/-/mime-1.2.11.tgz
npm http GET https://registry.npmjs.org/ee-first
npm http 200 https://registry.npmjs.org/ee-first
npm http GET https://registry.npmjs.org/ee-first/-/ee-first-1.0.3.tgz
npm http 200 https://registry.npmjs.org/ee-first/-/ee-first-1.0.3.tgz
express@4.6.0 /root/.nvm/v0.10.28/lib/node_modules/express
tqq merge-descriptors@0.0.2
tqq utils-merge@1.0.0
tqq parseurl@1.1.3
tqq cookie@0.1.2
tqq escape-html@1.0.1
tqq finalhandler@0.0.3
tqq cookie-signature@1.0.4
tqq range-parser@1.0.0
tqq fresh@0.2.2
tqq vary@0.1.0
tqq qs@0.6.6
tqq media-typer@0.2.0
tqq methods@1.1.0
tqq serve-static@1.3.2
tqq buffer-crc32@0.2.3
tqq depd@0.3.0
tqq path-to-regexp@0.1.3
tqq debug@1.0.3 (ms@0.6.2)
tqq proxy-addr@1.0.1 (ipaddr.js@0.1.2)
tqq accepts@1.0.7 (negotiator@0.4.7, mime-types@1.0.1)
tqq type-is@1.3.2 (mime-types@1.0.1)
mqq send@0.6.0 (ms@0.6.2, mime@1.2.11, finished@1.2.2)
バージョンを確認してみる
# express -V
-bash: express: コマンドが見つかりません
なぜか、コマンドが見つからない・・・Σ(゚д゚lll)ガーン
調べてみると express-generator というパッケージをインストールする必要があるそうです。

express-generatorをインストールする
# npm install -g express-generator
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
npm http GET https://registry.npmjs.org/mkdirp
npm http 200 https://registry.npmjs.org/commander
npm http 200 https://registry.npmjs.org/mkdirp
npm http GET https://registry.npmjs.org/commander/-/commander-1.3.2.tgz
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/-/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
/root/.nvm/v0.10.28/bin/express -> /root/.nvm/v0.10.28/lib/node_modules/express-generator/bin/express
express-generator@4.2.0 /root/.nvm/v0.10.28/lib/node_modules/express-generator
tqq mkdirp@0.3.5
mqq commander@1.3.2 (keypress@0.1.0)
再度、バージョンを確認してみる
# express -V
4.2.0
キタ――(゚∀゚)――!!
expressのオプションを確認
# express -h

  Usage: express [options] [dir]

  Options:

    -h, --help          output usage information
    -V, --version       output the version number
    -e, --ejs           add ejs engine support (defaults to jade)
    -H, --hogan         add hogan.js engine support
    -c, --css <engine>  add stylesheet <engine> support (less|stylus|compass) (defaults to plain css)
    -f, --force         force on non-empty directory

expressのインストールが終わったので、
こちらの記事で公開されているサンプルを実際に動かしてみたいと思います。
Node.js、Socket.IO、MongoDBでリアルタイムWeb (2/2)

ソースコードをダウンロードしてきます。
# git clone git://github.com/coppieee/node-chat-demo.git
Initialized empty Git repository in /root/node-chat-demo/.git/
remote: Counting objects: 32, done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 32 (delta 6), reused 32 (delta 6)
Receiving objects: 100% (32/32), 79.46 KiB, done.
Resolving deltas: 100% (6/6), done.
ディレクトリを移動します
cd node-chat-demo
依存関係のライブラリを設定します。
# npm install
npm http GET https://registry.npmjs.org/socket.io
npm http GET https://registry.npmjs.org/express
npm http GET https://registry.npmjs.org/ejs
npm http 304 https://registry.npmjs.org/express
npm http GET https://registry.npmjs.org/express/-/express-3.0.0rc1.tgz
npm http 304 https://registry.npmjs.org/socket.io
npm http 304 https://registry.npmjs.org/ejs
npm http GET https://registry.npmjs.org/ejs/-/ejs-1.0.0.tgz
npm http 200 https://registry.npmjs.org/ejs/-/ejs-1.0.0.tgz
npm http 200 https://registry.npmjs.org/express/-/express-3.0.0rc1.tgz
npm http GET https://registry.npmjs.org/socket.io-parser
npm http GET https://registry.npmjs.org/socket.io-adapter
npm http GET https://registry.npmjs.org/socket.io-client
npm http GET https://registry.npmjs.org/has-binary-data
npm http GET https://registry.npmjs.org/debug
npm http GET https://registry.npmjs.org/engine.io
npm http 304 https://registry.npmjs.org/debug
npm http GET https://registry.npmjs.org/connect
npm http GET https://registry.npmjs.org/crc
npm http GET https://registry.npmjs.org/commander
npm http GET https://registry.npmjs.org/cookie
npm http GET https://registry.npmjs.org/range-parser
npm http GET https://registry.npmjs.org/mkdirp
npm http GET https://registry.npmjs.org/send
npm http GET https://registry.npmjs.org/methods
npm http GET https://registry.npmjs.org/fresh
npm http 304 https://registry.npmjs.org/socket.io-adapter
npm http 304 https://registry.npmjs.org/socket.io-parser
npm http 304 https://registry.npmjs.org/engine.io
npm http 304 https://registry.npmjs.org/commander
npm http 304 https://registry.npmjs.org/socket.io-client
npm http 304 https://registry.npmjs.org/cookie
npm http 304 https://registry.npmjs.org/has-binary-data
npm http 304 https://registry.npmjs.org/range-parser
npm http GET https://registry.npmjs.org/cookie/-/cookie-0.0.4.tgz
npm http 304 https://registry.npmjs.org/mkdirp
npm http 304 https://registry.npmjs.org/methods
npm http GET https://registry.npmjs.org/range-parser/-/range-parser-0.0.4.tgz
npm http GET https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.3.tgz
npm http GET https://registry.npmjs.org/methods/-/methods-0.0.1.tgz
npm http 304 https://registry.npmjs.org/fresh
npm http 200 https://registry.npmjs.org/crc
npm http GET https://registry.npmjs.org/fresh/-/fresh-0.1.0.tgz
npm http GET https://registry.npmjs.org/crc/-/crc-0.2.0.tgz
npm http 304 https://registry.npmjs.org/send
npm http 200 https://registry.npmjs.org/connect
npm http 200 https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.3.tgz
npm http GET https://registry.npmjs.org/connect/-/connect-2.4.1.tgz
npm http GET https://registry.npmjs.org/send/-/send-0.0.3.tgz
npm http 200 https://registry.npmjs.org/range-parser/-/range-parser-0.0.4.tgz
npm http 200 https://registry.npmjs.org/methods/-/methods-0.0.1.tgz
npm http 200 https://registry.npmjs.org/crc/-/crc-0.2.0.tgz
npm http 200 https://registry.npmjs.org/send/-/send-0.0.3.tgz
npm http 200 https://registry.npmjs.org/fresh/-/fresh-0.1.0.tgz
npm http 200 https://registry.npmjs.org/cookie/-/cookie-0.0.4.tgz
npm http GET https://registry.npmjs.org/isarray
npm http 200 https://registry.npmjs.org/connect/-/connect-2.4.1.tgz
npm http GET http://github.com/component/emitter/archive/1.0.1.tar.gz
npm http GET https://registry.npmjs.org/json3
npm http 304 https://registry.npmjs.org/isarray
npm http GET https://registry.npmjs.org/engine.io-parser
npm http GET https://registry.npmjs.org/base64id
npm http GET https://registry.npmjs.org/ws
npm http 304 https://registry.npmjs.org/json3
npm http 304 https://registry.npmjs.org/ws
npm http 304 https://registry.npmjs.org/engine.io-parser
npm http 304 https://registry.npmjs.org/base64id
npm http GET https://registry.npmjs.org/component-bind
npm http GET https://registry.npmjs.org/object-component
npm http GET https://registry.npmjs.org/component-emitter
npm http GET https://registry.npmjs.org/indexof
npm http GET https://registry.npmjs.org/parseuri
npm http GET https://registry.npmjs.org/to-array
npm http GET https://registry.npmjs.org/engine.io-client
npm http GET https://registry.npmjs.org/utf8
npm http GET https://registry.npmjs.org/base64-arraybuffer
npm http GET https://registry.npmjs.org/arraybuffer.slice
npm http GET https://registry.npmjs.org/after
npm http GET https://registry.npmjs.org/blob
npm http GET https://registry.npmjs.org/mime
npm http GET https://registry.npmjs.org/ms
npm http 304 https://registry.npmjs.org/component-bind
npm http 304 https://registry.npmjs.org/component-emitter
npm http 304 https://registry.npmjs.org/indexof
npm http 304 https://registry.npmjs.org/object-component
npm http 304 https://registry.npmjs.org/parseuri
npm http 304 https://registry.npmjs.org/to-array
npm http GET https://registry.npmjs.org/nan
npm http GET https://registry.npmjs.org/tinycolor
npm http GET https://registry.npmjs.org/options
npm http 304 https://registry.npmjs.org/base64-arraybuffer
npm http GET https://registry.npmjs.org/formidable
npm http GET https://registry.npmjs.org/pause
npm http GET https://registry.npmjs.org/qs
npm http GET https://registry.npmjs.org/bytes
npm http 304 https://registry.npmjs.org/engine.io-client
npm http 304 https://registry.npmjs.org/mime
npm http 304 https://registry.npmjs.org/utf8
npm http 304 https://registry.npmjs.org/arraybuffer.slice
npm http GET https://registry.npmjs.org/mime/-/mime-1.2.6.tgz
npm http 304 https://registry.npmjs.org/after
npm http 304 https://registry.npmjs.org/tinycolor
npm http 304 https://registry.npmjs.org/options
npm http 200 https://registry.npmjs.org/pause
npm http 200 https://registry.npmjs.org/formidable
npm http 304 https://registry.npmjs.org/qs
npm http 304 https://registry.npmjs.org/blob
npm http 304 https://registry.npmjs.org/bytes
npm http GET https://registry.npmjs.org/pause/-/pause-0.0.1.tgz
npm http 200 https://registry.npmjs.org/mime/-/mime-1.2.6.tgz
npm http GET https://registry.npmjs.org/formidable/-/formidable-1.0.11.tgz
npm http GET https://registry.npmjs.org/qs/-/qs-0.4.2.tgz
npm http GET https://registry.npmjs.org/bytes/-/bytes-0.1.0.tgz
npm http 304 https://registry.npmjs.org/nan
npm http 200 https://registry.npmjs.org/pause/-/pause-0.0.1.tgz
npm http 304 https://registry.npmjs.org/ms
npm http 200 https://registry.npmjs.org/qs/-/qs-0.4.2.tgz
npm http GET https://registry.npmjs.org/better-assert
npm http 200 https://registry.npmjs.org/formidable/-/formidable-1.0.11.tgz
npm http 200 https://registry.npmjs.org/bytes/-/bytes-0.1.0.tgz
npm http 304 https://registry.npmjs.org/better-assert
npm http GET https://registry.npmjs.org/callsite

> ws@0.4.31 install /root/node-chat-demo/node_modules/socket.io/node_modules/engine.io/node_modules/ws
> (node-gyp rebuild 2> builderror.log) || (exit 0)

npm http 304 https://registry.npmjs.org/callsite
npm http GET https://registry.npmjs.org/has-cors
npm http GET https://github.com/LearnBoost/node-XMLHttpRequest/archive/0f36d0b5ebc03d85f860d42a64ae9791e1daa433.tar.gz
npm http GET https://registry.npmjs.org/parsejson
npm http GET https://registry.npmjs.org/parseqs
npm http GET https://registry.npmjs.org/component-inherit
npm http 304 https://registry.npmjs.org/parseqs
npm http 304 https://registry.npmjs.org/parsejson
npm http 304 https://registry.npmjs.org/has-cors
npm http 200 http://github.com/component/emitter/archive/1.0.1.tar.gz
npm http 304 https://registry.npmjs.org/component-inherit
npm http 200 https://github.com/LearnBoost/node-XMLHttpRequest/archive/0f36d0b5ebc03d85f860d42a64ae9791e1daa433.tar.gz
npm http GET https://github.com/component/global/archive/v2.0.1.tar.gz

> ws@0.4.31 install /root/node-chat-demo/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws
> (node-gyp rebuild 2> builderror.log) || (exit 0)

npm http 200 https://github.com/component/global/archive/v2.0.1.tar.gz
ejs@1.0.0 node_modules/ejs

express@3.0.0-rc1 node_modules/express
tqq methods@0.0.1
tqq fresh@0.1.0
tqq range-parser@0.0.4
tqq cookie@0.0.4
tqq crc@0.2.0
tqq commander@0.6.1
tqq mkdirp@0.3.3
tqq debug@1.0.3 (ms@0.6.2)
tqq send@0.0.3 (mime@1.2.6)
mqq connect@2.4.1 (pause@0.0.1, bytes@0.1.0, qs@0.4.2, formidable@1.0.11)

socket.io@1.0.6 node_modules/socket.io
tqq debug@0.7.4
tqq has-binary-data@0.1.1 (isarray@0.0.1)
tqq engine.io@1.3.1 (base64id@0.1.0, debug@0.6.0, engine.io-parser@1.0.6, ws@0.4.31)
tqq socket.io-adapter@0.2.0 (socket.io-parser@2.1.2)
tqq socket.io-parser@2.2.0 (isarray@0.0.1, emitter@1.0.1, json3@3.2.6)
mqq socket.io-client@1.0.6 (to-array@0.1.3, indexof@0.0.1, component-bind@1.0.0, object-component@0.0.3, component-emitter@1.1.2, parseuri@0.0.2, engine.io-client@1.3.1)
node.jsを実行します
# node app
Express server listening on port 3000
ブラウザから3000ポートにアクセスします。
http://ドメイン:3000/

ブラウザでアクセスすると、下の画像のような画面が開かれますので、
タブなどで同じURLの画面を複数個開きます。
(別々のマシンでも可能)



文字を入力して「send」を押すと、
各ブラウザに送信されたメッセージが表示されれば正常に動作しています。


以上です(`・ω・´)ゞビシッ!!

参考URL

0 件のコメント:

コメントを投稿