#contents *Node.jsのご利用方法 [#b30100f6] nodeコマンドで作成したスクリプトを実行して下さい。~ 例)% node app.js **Hello, world!と出力するHTTPサーバスクリプトの例 [#f2ff41e1] +ポート番号3000で動作します。次の内容をテキストエディタで作成して、~ httpd.jsというファイル名で保存して下さい。 (ポート番号はwell-knownポート内で自由に変更して下さい)~ var http = require('http'); var port = 3000; var server = http.createServer(function(req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello, world!\n'); }); server.listen(port); console.log('Server running *:' + port); +nodeコマンドでhttpd.jsを実行して下さい。 % node httpd.js Server running *:3000 +ウェブブラウザから http://お客様のドメイン:3000 に接続し、~ Hello, world!という文字列が表示されていれば正常に実行されています。 *npmコマンド [#vce0764b] socket.ioなどのパッケージをインストールできます。~ **初期設定 [#i40a7c8d] +スクリプト等を設置するディレクトリを用意してその中に移動して下さい。 % mkdir nodejs % cd nodejs +npm initを実行して名前等を自由に入力して下さい。 % npm init This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sane defaults. See `npm help json` for definitive documentation on these fields and exactly what they do. Use `npm install <pkg> --save` afterwards to install a package and save it as a dependency in the package.json file. Press ^C at any time to quit. name: (node2) version: (1.0.0) description: entry point: (index.js) test command: git repository: keywords: author: license: (ISC) About to write to /home/22/ex1200/node2/package.json: { "name": "node2", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" } Is this ok? (yes) +package.jsonというファイルが作成されれば完了です。 **パッケージのインストール [#sde457ff] npm installでインストールできます。 % npm install パッケージ名 *socket.ioのご利用方法 [#k9742fec] インストールされておりませんのでnpmコマンドを使ってインストールして下さい。 **インストール [#r79d6496] npm installコマンドを使用してインストールすることが出来ます。 +package.jsonを作成したディレクトリにいない場合は、そこに移動して下さい。 % cd nodejs +npmコマンドでsocket.ioをインストールして下さい。 % npm install socket.io npm WARN package.json node2@1.0.0 No description npm WARN package.json node2@1.0.0 No repository field. npm WARN package.json node2@1.0.0 No README data > ws@0.4.31 install /home/22/ex1200/node2/node_modules/socket.io/node_modules/engine.io/node_modules/ws > (node-gyp rebuild 2> builderror.log) || (exit 0) gmake: ディレクトリ `/home/22/ex1200/node2/node_modules/socket.io/node_modules/engine.io/node_modules/ws/build' に入ります CXX(target) Release/obj.target/bufferutil/src/bufferutil.o SOLINK_MODULE(target) Release/obj.target/bufferutil.node COPY Release/bufferutil.node CXX(target) Release/obj.target/validation/src/validation.o SOLINK_MODULE(target) Release/obj.target/validation.node COPY Release/validation.node gmake: ディレクトリ `/home/22/ex1200/node2/node_modules/socket.io/node_modules/engine.io/node_modules/ws/build' から出ます > ws@0.4.31 install /home/22/ex1200/node2/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) gmake: ディレクトリ `/home/22/ex1200/node2/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/build' に入ります CXX(target) Release/obj.target/bufferutil/src/bufferutil.o SOLINK_MODULE(target) Release/obj.target/bufferutil.node COPY Release/bufferutil.node CXX(target) Release/obj.target/validation/src/validation.o SOLINK_MODULE(target) Release/obj.target/validation.node COPY Release/validation.node gmake: ディレクトリ `/home/22/ex1200/node2/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-client/node_modules/ws/build' から出ます socket.io@1.1.0 node_modules/socket.io ├── debug@0.7.4 ├── has-binary-data@0.1.3 (isarray@0.0.1) ├── socket.io-parser@2.2.1 (isarray@0.0.1, component-emitter@1.1.2, benchmark@1.0.0, json3@3.2.6) ├── socket.io-adapter@0.2.0 (socket.io-parser@2.1.2) ├── engine.io@1.4.0 (base64id@0.1.0, debug@1.0.3, engine.io-parser@1.1.0, ws@0.4.31) └── socket.io-client@1.1.0 (to-array@0.1.3, indexof@0.0.1, component-bind@1.0.0, object-component@0.0.3, component-emitter@1.1.2, has-binary@0.1.5, parseuri@0.0.2, socket.io-parser@2.2.2, engine.io-client@1.4.0) +カレントディレクトリにnode_modulesディレクトリが作成されsocket.ioがインストールされていれば完了です。 **socket.ioを利用したチャットプログラムの例 [#tf0db073] 公式サイトのチュートリアル(http://socket.io/get-started/chat/)を当社サーバで作成する例です。~ +package.jsonを作成したディレクトリにいない場合は、そこに移動して下さい。 % cd nodejs +npmコマンドでexpressをインストールして下さい。 ※ --saveオプションをつけて下さい。 % npm install --save express npm WARN package.json node2@1.0.0 No description npm WARN package.json node2@1.0.0 No repository field. npm WARN package.json node2@1.0.0 No README data express@4.9.7 node_modules/express ├── utils-merge@1.0.0 ├── merge-descriptors@0.0.2 ├── fresh@0.2.4 ├── cookie@0.1.2 ├── escape-html@1.0.1 ├── range-parser@1.0.2 ├── cookie-signature@1.0.5 ├── finalhandler@0.2.0 ├── vary@1.0.0 ├── media-typer@0.3.0 ├── methods@1.1.0 ├── parseurl@1.3.0 ├── serve-static@1.6.4 ├── path-to-regexp@0.1.3 ├── depd@0.4.5 ├── on-finished@2.1.0 (ee-first@1.0.5) ├── qs@2.2.4 ├── debug@2.0.0 (ms@0.6.2) ├── proxy-addr@1.0.3 (forwarded@0.1.0, ipaddr.js@0.1.3) ├── etag@1.4.0 (crc@3.0.0) ├── send@0.9.3 (destroy@1.0.3, ms@0.6.2, mime@1.2.11) ├── accepts@1.1.2 (negotiator@0.4.9, mime-types@2.0.2) └── type-is@1.5.2 (mime-types@2.0.2) +次の内容をテキストエディタで作成して、index.htmlというファイルで保存して下さい。 <!doctype html> <html> <head> <title>Socket.IO chat</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { font: 13px Helvetica, Arial; } form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; } form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; } form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; } #messages { list-style-type: none; margin: 0; padding: 0; } #messages li { padding: 5px 10px; } #messages li:nth-child(odd) { background: #eee; } </style> </head> <body> <ul id="messages"></ul> <form action=""> <input id="m" autocomplete="off" /><button>Send</button> </form> <script src="/socket.io/socket.io.js"></script> <script src="http://code.jquery.com/jquery-1.11.1.js"></script> <script> var socket = io(); $('form').submit(function(){ socket.emit('chat message', $('#m').val()); $('#m').val(''); return false; }); socket.on('chat message', function(msg){ $('#messages').append($('<li>').text(msg)); }); </script> </body> </html> +次の内容をテキストエディタで作成して、index.jsというファイルで保存して下さい。 var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); app.get('/', function(req, res){ res.sendfile('index.html'); }); io.on('connection', function(socket){ socket.on('chat message', function(msg){ io.emit('chat message', msg); }); }); http.listen(3000, function(){ console.log('listening on *:3000'); }); +nodeコマンドでindex.jsを実行して下さい。 % node index.js listening on *:3000 +ウェブブラウザから http://お客様のドメイン:3000 に接続し、~ チャット画面が表示されていれば正常に実行されています。~ ブラウザを2個起動しそれぞれの画面からアクセスして、同時に発言されるかどうかなど動作をご確認下さい。~