入門
- node.jsをはじめました (10 Oct 2012 | Tags:
node.jsをはじめました node.jsをはじめました
謝辞
今回、node.jsをはじめるに辺りこちらのサイトを参考にさせていただきました。感謝です。
インストール
まずは、環境の準備です。
上記ハンズオン資料にもUnixシステムへのインストール手順が記載されていますが、手持ちの環境がWindowsなので若干異なりました。
1. インストーラのダウンロード
下記サイトから最新の安定版をダウンロード。2012/10/10時点の最新の安定版は、v0.8.11
2. インストーラの起動
画面に従いインストールを完了します。
3. インストールの確認
コマンドラインからインストールしたnode.jsのバージョンを確認します。v0.8.11の表示が確認できます。
$ node -v v0.8.11
4. npmのインストール npmは、nodeのライブラリパッケージ専用のパッケージ管理ツールです。 インストーラからインストールすると、併せてnpmもインストールされます。
Hello World
helloworld.jsを作成します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersfunction hello(name){ console.log("Hello " + name); } hello('node.js'); 実行すると、こんな感じ。
$ node helloworld.js Hello node.js
非同期IOメソッドを使う
ハンズオンの内容を写経してみます。
1ファイルのダウンロード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters// モジュールの読み込み var util = require('util'); url = require('url'); http = require('http'); // ダウンロードを実行する関数 function download(urlStr) { // URL文字列のパース var u = url.parse(urlStr); // http.clientRequestオブジェクトの組み立て var client = http.createClient(u.port || 80, u.hostname); var request = client.request('GET', u.pathname, {host: u.hostname}); // リクエストの送信終了 request.end(); // イベントハンドラの定義 request.on('response', function(response) { // ステータスコードとヘッダーの出力 console.log(response.statusCode); for(var i in response.headers) { console.log(i + ":" + response.headers[i]); } console.log(''); // データ取得イベントの非同期処理 response.setEncoding('utf8'); response.on('data', function(chunk) { // chunkは受信したデータ(デフォルトでUTF8エンコード) util.print(chunk); }); // レスポンス終了イベントの非同期処理 response.on('end', function() { console.log(''); }); }); } download(process.argv[2]); これを実行すると、こんな感じ。
$ node download1.js http://www.google.co.jp/ http.createClient is deprecated. Use `http.request` instead. 200 date:Wed, 10 Oct 2012 21:17:40 GMT expires:-1 cache-control:private, max-age=0 content-type:text/html; charset=Shift_JIS set-cookie:PREF=ID=e0429b2fdb66b6f5:FF=0:TM=1349903860:LM=1349903860:S=RztyFRMdq8JPKHw1; expires=Fri, 10-Oct-2014 21:17:40 GMT; path=/; domain=.google.co.jp,NID=64=bF0HOrYnQ_hHT1a9O-_qkflB9ZOIv34SmPnV4YeXbEbhqCQGR0wjpKvjFPtnQ0psZzJQPW3qZvP7uaHjxg_ajr0--fMSSVCFvOzHFe_O6Eqc2wCs-VZqV-7L3-1PnQ9V; expires=Thu, 11-Apr-2013 21:17:40 GMT; path=/; domain=.google.co.jp; HttpOnly p3p:CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info." server:gws x-xss-protection:1; mode=block x-frame-options:SAMEORIGIN transfer-encoding:chunked ・・・
実行結果の1行目に何やらメッセージが出力されていますが、リファレンスで確認するとhttp.createClientの使用は非推奨とのことでした。
http.request()で書き直したものがこちら。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters// モジュールの読み込み var util = require('util'); url = require('url'); http = require('http'); // ダウンロードを実行する関数 function download(urlStr) { // URL文字列のパース var options = url.parse(urlStr); // http.clientRequestオブジェクトの組み立て var request = http.request(options, function(res) {}); // リクエストの送信終了 request.end(); // イベントハンドラの定義 request.on('response', function(response) { // ステータスコードとヘッダーの出力 console.log(response.statusCode); for(var i in response.headers) { console.log(i + ":" + response.headers[i]); } console.log(''); // データ取得イベントの非同期処理 response.setEncoding('utf8'); response.on('data', function(chunk) { // chunkは受信したデータ(デフォルトでURF8エンコード) util.print(chunk); }); // レスポンス終了イベントの非同期処理 response.on('end', function() { console.log(''); }); }); } download(process.argv[2]); 同じように動きます。
Latest post:
- OpenWhiskのScala sbtプロジェクトのgiter8テンプレートを作った
- OpenWhisk+Scalaで作るServerless Architectureとっかかり
- BluemixにPlayframeworkアプリケーションをデプロイする
- sbt、Giter8を統合するってよ
- Scala 2.12.0でSAM型
Recent Books:
Jekyll::Drops::SiteDrop