node.js 使ã£ã¦ã¿ã
ãªãããªããã¾ããµã(èªã¿é£ã°ãæ¨å¥¨)
(é¢æ°åè¨èªä½¿ã£ã¦ãªãããã©)æè¿ãããã°ã©ã ãæ¸ãã¨ãã«ã¯é¢æ°åè¨èªã§æ¸ãããããã¯é¢æ°åã£ã½ãæ¸ãã¨ããããèãããã¨ãæ¸ã£ã¦ããããããªããã¨æã£ã¦ãã¦ãããã¨é¢é£ã㦠Erlang ã¨ãã®ã¢ã¯ã¿ã¼ã¢ãã«ãç§ã®æãã§ããã¹ã¿ã¤ã«ã«ã´ã£ãããªããããªããã¨æã£ã¦ããããã§ãã
ã¨ããã®ã¯ãthread ã¨ã使ã£ã¦ãæã« mutex lock ã¨ãããªãã¡ããããªãã¨ãããããèããããªãããã§ãããããã£ã¦é¢æ°åè¨èªã§ããè¨ããããå¯ä½ç¨ãç¡ããã¨ããç¶æ
ã«ãã¦ããã°ããããã mutex lock ãªãã¦æ¦å¿µã¯ä½¿ããªãã¦ããããã§ãããã¨ãã°ãäºæé¨å¼ã«ãã£ã¦ããè¦æ±ã«å¯¾ãã¦åå¥ã« thread ãç«ã¦ã¦å¯¾å¿ããããã£ã¦ãªäºã¯ããããããã§ããããã®æä½ã thread ãå¯ä½ç¨ã®ç¡ããã®ã«ãã¦ãã㨠mutex lock ã¨ãããªãã¦æ¸ãã§ããã§ããã == èããäºæ¸ããããã¨ãªãããã§ãã
ã¨ãã£ã¦ããããã ãã§ã¯ãã¾ãåããªãã¦ãå¯ä½ç¨ã®ããäºãæ¸ããããªã£ããããããã§ããä¾ãã°ãè¤æ°ã®ã¢ã¯ã»ã¹ãåæã«æ¥ãã¨ãã«ããã®ãã¡ä¸ã¤ã ãã«ä½ãç¹å¥ãªå¤ãè¿ãããæã§ãããã®ä¾ã ã¨ãç¹å¥ãªå¤ãè¿ããäºããããã©ãããè¨æ¶ãã¦ããé¨åããå
±æãã¦ãæ¸ãæããªãã¨ãããªããã¨ãããã¨ã§ãå¯ä½ç¨ãä¼´ãäºã«ãªãã¾ããããã§ãã¢ã¯ã¿ã¼ã¢ãã«ã ã¨ã¢ã¯ã¿ã¼éã§ã®ã¡ãã»ã¼ã¸ããã·ã³ã°ã¨ããä»çµã¿ã§ãã®å¯ä½ç¨ãããç¨åº¦è§£æ¶ã§ãã¦ããããããªãããªãã¨æã£ã¦ãã¾ãã
ã¨ããã®ã¯ãã¢ã¯ã¿ã¼ã¢ãã«ã ã¨ã¢ã¯ã¿ã¼ãã¨ã«ç¶æ
ãããäºã«ãªãâ¦â¦ã®ããªããã¶ããâ¦â¦ã®ã§ãããã¨ããããå¯ä½ç¨ã®ç¡ãé¨åã¯é¢æ°ã®å½¢ã§å®è£
ãã¦ãå¯ä½ç¨ãçºçããæã§ä»ã®ã¢ã¯ã¿ã¼ã«å¯¾ãã¦ã¡ãã»ã¼ã¸ãæãããã¨ããåä½ã«ãã¦ãã¾ãã°ãåã
ã®é¢æ°ã«é¢ãã¦ã¯å¯ä½ç¨ã®ç¡ããã®ãæ¸ããã®ããªããã¨ããæ°ããã¦ãã¾ããä¾ãã°ãåã®ä¾ã ã¨è¤æ°ã®ãªã¯ã¨ã¹ããåããæã«ãã®ãªã¯ã¨ã¹ãã®æ°åã ãã¢ã¯ã¿ã¼ A, B, C ãä½ã£ã¦ããããã®ã¢ã¯ã¿ã¼ A, B, C ãããä¸ã¤ã®ã¢ã¯ã¿ã¼ α ã«ã¡ãã»ã¼ã¸ãæããã¨ããã®ã¢ã¯ã¿ã¼ α ãæã£ã¦ããå
é¨ç¶æ
ãæ´æ°ãã¤ã¤ãã¢ã¯ã¿ã¼ A, B, C ããããã«å¥ã®ã¡ãã»ã¼ã¸ãæãè¿ã(ãã¡ãä¸ã¤ã¯ç¹å¥ãªå¤ãè¿ã)ãã¨ããäºã§å®è£
ããã°ãããã¨ããäºã«ãªãã¾ã(è足: ãã®å ´åã¯ã¢ã¯ã¿ã¼ α ã¯å¯ä½ç¨ãããé¢æ°ãå®è£
ãã¦ãããã¨ã«ãªãã¾ããå®éã®æãErlang ã®åæ£DB Mnesia ã¯ããããæãã§åãã¦ããã£ã½ãã§ã)ã
ã¨ãããã¨ã§ããå¯ä½ç¨ããªãé¢æ°ãæ¸ãããã¨ããã®ã¨ããã¢ã¯ã¿ã¼ã¢ãã«ã£ã¦ã¤ã¤ã«ã¢ï¼ãã¨ããã®ãæè¿ç§ããã¶ãã¦ããæ¦å¿µãªããã§ãã
ããErlang ã¨ãããé¢æ°åè¨èªèªä½ãå ¨ç¶ä½¿ã£ã¦ããªããããããã¾ã§æ¸ãã¦ããäºã¯å ¨é¨ç§ã®ç解ããã¤ããã«ãªã£ã¦ããã«ããç¥èããã®æ³åã§ããã¨ãããã¨ã§åå «ç¾ãæ¸ãã¦ãããããããªãã®ã§éµåã¿ã«ããã¨å¤§å¤ãªãã¨ã«ãªããããããã¾ãããã¨ãããããããã£ã¦ã人ã¯ã³ã¡ã³ãæ¬ã§ã§ãééããææãã¦æãããã¦ããã¦é ããã¨å¬ããã§ãããã¡ããããã£ããæãã¦ããã ãã®ã§ãæ§ãã¾ããã:D
ã§ãã§ããErlang ãããããã Mnesia ã¯çã®åæ£DBã ã(CodeZineã®è¨äºã®åé ã«ãã)ãåããããããæå¼·ãªããããï¼ã¨æã£ã¦ Erlang ã使ã£ã¦ã¿ããããªãã¨æã£ãã®ã ããã©ãããããã¨è¦ããªããã°ãªããªããã¨ãå¤ãã¦å¤§å¤ã ãªããã¨æãã¦ãããã¦ããªãã£ãæã«ããnode.js ã£ã¦ãªã®ããã£ã¦ã¼ãã¨ãã話é¡ãèãã¤ããããnode.js ã«ã¤ãã¦ã® Erlang ã®ã³ãã¥ããã£ã®åå¿ ã¿ãããªä¾ãè¦ãããã¦ãæ¡å¤ node.js ããããããããããï¼ãªããå¢ãããã¿ããã£ã¦æã¨ãæç¶ãåã£ã½ãæ¸ãæ¹ã¨ãããã§ãã¯ãã¼ã¸ã£ãæ¸ããã®ã§ã¡ãã»ã¼ã¸ããã·ã³ã°ã£ã½ãä½ãã¨ãã¦éåæI/Oãé è½ãã¦ãããç§ã¨ãã¦ã¯ JavaScript ãªã Erlang ããæ¸ããããããã¨ãèãã¦ãnode.js ãæ°ã«ãªã£ã¦ããããã§ãã
ã¨ãããã¨ã§ãnode.js ãã¨ãããã使ã£ã¦ã¿ããã¨ãã¦ã¿ã¾ããããã¨ããã話ã§ãã
node.js ã¨ã¯
node.js ã¯ãJavaScript ã§æ¸ãããµã¼ããã¤ããããã®ä½ãã®ãã¡ã®ä¸ã¤ãã®ããã§ãã以ä¸ã®ãããªç¹å¾´ãæã¡ã¾ãã
- éåæI/O ã®å¡ãªã®ã§ãåºæ¬çã«ã¯ãã¼ã¸ã£ã使ã£ã¦ã®é¢æ°å¼ã³åºãã®é£éã§ã¢ããªã±ã¼ã·ã§ã³ãè¨è¿°ãã(ãã¶ãããã®ããã㯠jQuery ã使ã£ã¦ã人ãã¡ã¨ãã¯é¦´æã¿ãããæ°ããã)
- V8 JavaScript Engine ã使ã£ã¦ããã®ã§ JavaScript ã¨ãã£ã¦ãé ãããã§ã¯ãªã
- libev ã使ã£ã¦ éåæI/O ãå®ç¾ãã¦ãã(å¤å C10Kåé¡ ã¸ã®å¯¾å¿ã¯OKãªæ°ããã)
- JavaScript ã¨ããã ããã£ã¦ãWebå±ããæ¹é¢ã®äººã ã¨ããæ¼ãå¯ãã¦ãã¦ããããããæ´»æ°ãããã£ã½ãï¼
- npm ã¨ããã¢ã¸ã¥ã¼ã«ããã¼ã¸ã£ããã£ã¦ããªã«ãã便å©ãªã¢ã¸ã¥ã¼ã«ããã£ã±ããã(sudo curl http://... | sh ã£ã¦ããã¨ã¤ã³ã¹ãã¼ã«å®äºã£ã¦ã©ããªã®ãâ¦â¦ ã¨ãããã®ã¯ã¡ãã£ã¨æ°ã«ãªãã±ã)
éçºç°å¢ãæã«å ¥ãã
node.js æ¬ä½ãæã«å ¥ãã
http://nodejs.org/ ããå æ¥ãªãªã¼ã¹ããã v0.4.0(stable release 㯠0.(n*2).* (n 㯠0, 1, 2, ...)ãããã§ã)ã download ãã¦ãã¦
% ./configure; make; sudo make install
㧠install ã¯å®äºã§ãã
ããmake ããã®ã« python ã¨ã libopenssl ã¨ããããã¨ããã§ã(python ã¯å¿
é )ã
ä½æ
ãæå
ã® Windows7 64bitç ã§ã¯ configure ã®æç¹ã§ python ãå¤ãªã¨ã©ã¼ãåãã¦é§ç®ã§ãããã¾ã localhost ã§åãå¿
è¦ã¯ãªããªã£ãã®ã§ããã§ãããã©ãã
npm ãæã«å ¥ãã
npm 㯠node.js ã®ã¢ã¸ã¥ã¼ã«ãã¤ã³ã¹ãã¼ã«ããããããããã®ãã®ã§ãã
% curl http://npmjs.org/install.sh | sudo sh
ã§ãinstall ã¯çµäºãªãã§ããâ¦â¦ ãããã¯ã¼ã¯ã®å
ã® sh script ããã®ã¾ã¾ sudo ãã sh ã«é£ãããã®ã£ã¦â¦â¦ ã¨æãã¾ããã
ã§ãããã®å¾ã§
% sudo npm install express
ã¨ããã¡ãã£ã¦ãã®ã§ããã¤ã¤ã§ããããã¶ãã
node-inspector ãã¤ã³ã¹ãã¼ã«ãã
node-inspector 㯠node.js ã®ãããã°ãã§ããããã«ããä½ãã§ãã
ãã«ãºã£ã¨ããã® node.js ã§ã®ãããã°æ¹æ³ ã¨ããè¨äºãåãããããã£ãã®ã§ãããèªãã§ããã¨ããæ°ããã¾ãã
ãã«ãºã£ã¨ããã®æã§ãæ¸ããã¦ããã®ã§ããããã®ãããã¬ã¯ Google Chromeç ã® Webkitã使ã£ããã©ã¦ã¶ãå¿ è¦ã§ãã
ãã«ãºã£ã¨ããã®è¨äºã®æç¹ã§ã¯ 0.4.* ã¯åºã¦ããªãã£ãã®ã§å°ãããªããã¼ãªäºããã¦ããããã§ããããã 0.4.* ãåºã¦ããã®ã§ä½ãèããã«
% sudo npm install node-inspector
ã¨ããã ãã§è¯ãã§ãã
ãã®ä»ã使ããããªã¢ã¸ã¥ã¼ã«ãããã¤ãå ¥ãã
ä»åã®è¨äºé¨åã§ã¯ä½¿ãã¾ããã§ããããgoogleå ç ã«ããããèãã¦ããæãexpress ã¨ãããã¬ã¼ã ã¯ã¼ã¯ã ejs ã¨ãããã³ãã¬ã¼ãã¨ã³ã¸ã³ãsocket.io ã便å©ãããªã®ã§ãnpm ã§å ¥ãã¦ããã¾ããã
% sudo npm install express % sudo npm install ejs % sudo npm install socket.io
socket.io ã«ã¤ãã¦ã¯ãã¯ã©ã¤ã¢ã³ãå´ã®ãã®ãå¿ è¦ã£ã½ãã§ãã
ã¨ãããããããã°å®è¡ãã¦ã¿ã
ãããã°ç°å¢ã® node-inspector ã®ä½¿ãæ¹ãå°ãããªããã¼ã§ãããã¨ããã®ã¯ã
- node-inspector 㯠node ã¨ã¯å¥ã«èµ·åãã¦ããå¿ è¦ããã
- node-inspector 㯠localhost ããã®æ¥ç¶ããåãä»ããªãã®ã§ãchrome ã®åãã¦ãã host ã¨å¥ã® host ãã使ãã«ã¯ ssh ã® port forward ã®ãããªäºãããå¿ è¦ããã
ã¨ããç¹ã§ããã
ç§ã®å ´åã
localhost% ssh -L 8080:localhost:8080 node-inspectorã®åãã¦ãããã¹ã
ã¨ãã形㧠ssh 㧠port forward ããã¦ãhttp://localhost:8080 ããããã°ç¨ã«ã²ããã¦è§£æ±ºãã¾ããã
ã¨ãããã¨ã§ãèµ·åããé çªã¨ãã¦ã¯ã
% node-inspector &
% node --debug hoge.js
ã¨ãããããªæã㧠node-inspector 㨠node.js ãå®è¡ãã¦ãchrome 㧠http://localhost:8080/debug?port=5858 ã¨ã(default ãªã http://localhost:8080 㧠ok)ãéãã¨ãããã¬ã®ç»é¢ã表示ããã¾ããã
ãã¬ã¼ã¯ãã¤ã³ãã§æ¢ãããã¨ãå¤æ°ã®ä¸èº«ã¨ãã詳ããã¿ããã¾ãã
ãããããã§ãï¼
ã¡ãªã¿ã«ãè¤æ°äººã§åæã«åããã¹ãã§éçºããæã¨ãã§ã¯ãã¼ãçªå·ããã¶ã£ã¦ãã¾ãããã§ããããã®å ´å㯠node --debug ã¸ã®å¼æ°ã¨ãã¦ãããã°ç¨ã®ãã¼ãçªå·ãæå®ãã¦ãnode-inspector ã«ã¯ URL ã®å¾ãã« /debug?port=... ã®å½¢å¼ã§ãã®ãããã°ã®ãã¼ãçªå·ãæå®ããã°è¯ãããã§ãã
å
·ä½çã«ã¯ã
% node-inspector &
% node --debug=15858 hoge.js
ã¨ããã®ãªããhttp://localhost:8080/debug?port=15858 ã¸ã¨ã¢ã¯ã»ã¹ããã°ããããã¬ã¸ã¨æ¥ç¶ã§ãã¾ãã
ã¨ãããããããã§åæè¨å®ã¨ãã¦ã¯ããæãã§ããããããã¾ãä½ããä½ãããç¶ããããããã¾ããã