node.js/express 㧠CouchDB ããã¯ã¼ã¢ãã大ä½æ¦
CouchDB ãã¡ã¤ã³ã®ã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã¼ã¨ãã¦ä½¿ã£ã¦ãããã®ã®ããµã¼ãã¼ãµã¤ãã§ä»ã®ãµã¼ãã¹ã¨é£æºãããã¨ãã«ã¯ã¡ãã£ã¨ä¸ä¾¿ããæãã¦ãã¾ãã¾ãã
ããããã¨ãã¯ãã¤ããã?ã®Web Frameworkã®ä¸ã§ãCouchDBããã¼ã¿ãã¼ã¹ã¨ãã¦æ±ãã°è¯ããã§ãããCouchDB ã®ã¢ããªã¨å¯ã«é£æºãã(ä¸é¨ã®æ©è½ã®ããã ãã ãã)ããã«ã¯è¨èªãåãã«ããæ¹ãè¯ãã ãããã¨ãããã¨ã§ãnode.js ã使ãå§ãã¦ãã¾ãã
ã¨ãããããèªè¨¼ãtwitterã«ä»»ãããã
CouchDBã§ã¢ããªä½ã£ãå ´åãã¦ã¼ã¶ã¼DBèªä½ã¯CouchDBã«ä¿æããã¾ãããããã¯ã¡ãã£ã¨ãã ãCouchDBã®ã¤ã³ã¹ã¿ã³ã¹æ¯ã«ã¦ã¼ã¶ã¼æ±ããã¨ãããã
ã§ãã¤ããããã¨ãããã¨ã§ãtwitter ãèªè¨¼ãµã¼ãã¼ã¨ãã¦ä½¿ããã§ããOAuth ãã§ããã°OKã£ã¦ãã¨ã§ã
PHPã¨ãPythonã¨ãRubyã¨ãJavaã¨ããGAEã¨ããããã©ãServer Side JavaScript ã®ãµã³ãã«ããã¼YO! ã£ã¦ãã¨ã§ãæ¸ãã¦ã¿ã¾ããã
å®è£ ã®ä¸èº«
gist ã«ããã¨ãã¾ãããServer Side JavaScriptã¯IEãããªãããæ¸ãã¦ãã¦æ°æã¡ãè¯ãã§ããã
å¿ è¦ãªã®ã¯ node.js 㨠Express ã§ãã token ä¿æã®ã»ãã·ã§ã³å¨ãã¨ãé¢åã ã£ãã®ã§ Express ã«é ¼ãã¾ãããExpress 㯠Ruby ã® Sintra ã« inspire ããããnode.js ç¨ Web Framework ã ããã§ãããã¨ãConnect ã¨ãã node.js ç¨ã®ããã«ã¦ã§ã¢ã使ãã¾ãã
- ãªã¯ã¨ã¹ããã¼ã¯ã³ã®çºè¡ (/login)
- ã¢ã¯ã»ã¹ãã¼ã¯ã³ã®çºè¡ (/authorized)
- ã¢ã¯ã»ã¹ãã¼ã¯ã³ãå©ç¨ããAPIã¢ã¯ã»ã¹ (/twitter)
ã¨ããæãã§ããå®éã«ã¯ oauth ã¢ã¸ã¥ã¼ã«ã¯ç¬ç«ããã©ã¤ãã©ãªã¨ãã¦node.jsã«ããã®ã§ç°¡åã«å®è£ ã§ãã¾ããã ãã®oauthã©ã¤ãã©ãªã¯èªè¨¼ç¨URIã«å¯¾ãã¦ãQueryString以å¤ã«(ãã¨ãã°Request Bodyã«JSONãåãè¾¼ãã¨ã)ãªã¯ã¨ã¹ããéããã¨ãã§ããªããã¨ããå®è£ ä¸ã®å¶ç´ã¯ãããã®ã®ãæ®éã«ä½¿ãåã«ã¯åé¡ãªãããã§ãã
ã½ã¼ã¹ã³ã¼ããä¸éãè¦ã¾ãããJavaScriptã§ãé常ã«ããããããã
npm ã¢ã¸ã¥ã¼ã«ãå ¬éãã¦ã¿ã
ããä¸ã¤ãnode.js ã«ã¯ npm ã¨ãã Ruby ã® gemãPython ã® setuptools/pypiãPerl ã®CPANãPHPã®... çãªã¢ã¸ã¥ã¼ã«ãããã¾ãã
æ¨ä»ã使ãã°ã£ãããããã¡ã ãã¨æã£ã¦ããã®ã§ãã¨ããããèªåãããã«å¿ è¦ã ã£ãHTMLの本文抽出ライブラリã node.js ç¨ã® JavaScript ã«ç§»æ¤ãã¦å ¬éãã¦ã¿ã¾ãããå®å ¨ã«ç§»æ¤ã§ãããã©ããã¯æªããã§ãã(æ£è¦è¡¨ç¾å¨ãããã¾ã注ææ·±ãè¦ã¦ããªãã§ã)ãgithub ã¯ä»¥ä¸ã
http://github.com/yssk22/extractcontent
npm ã®æ¹ã«ãç»é²ãã¦ãããã®ã§ããã®èª²ç¨ãè¨ãã¾ãã
ã¾ãã¯ä½ãã package.json
ããã±ã¼ã¸æ å ±ã¯ package.json ã¨ãããã¡ã¤ã«ã«è¨è¿°ãã¾ããtar.gz ãå±éãã¦ã§ãããã£ã¬ã¯ããªã«ããã°ãããããªã®ã§ãé常 ããã¸ã§ã¯ãã®ã«ã¼ããã£ã¬ã¯ããªã«å¥¥ã§ãããã
http://github.com/yssk22/extractcontent/blob/master/package.json
ãããªæãã§ã©ã¤ãã©ãªã®å 容ãæ¸ãã¦ããã¾ãã
"main" : "./lib/extractcontent"
ããããå ¬éããã¨ã³ããªãã¤ã³ãã«ãªãã®ã§ãrequire('packagename') ããã¨ãã«ã¯ããã®ãã¡ã¤ã«ã§ export ããã¦ãããã®ãã©ã¤ãã©ãªã¨ãã¦ä½¿ãã¾ããä»å㯠extractFromUrl 㨠extractFromText ã¨ããäºã¤ã®é¢æ°ã extractcontent.js 㧠export ãã¦ããã¾ããã
ã¤ã³ã¹ãã¼ã«ã§ããã確èªã
$ npm install .
ããã§ãã«ã¬ã³ããã£ã¬ã¯ããªã® package.json ãèªãã§ã¤ã³ã¹ãã¼ã«ãããã¨ãã¾ãã
npm ok ã£ã¦ã§ãã°OKã§ãä¸å¿ãã®å¾ã§ã/usr/local/lib/node/extractcontent ã«ãã¡ã¤ã«ãå±éããã¦ãããã¨ã確èªãã¦ã
$ node > require('extractcontent').extractFromUrl('http://www.yssk22.info/top');
ã¨ããã¦ç¢ºèªãã¾ããã
ããã±ã¼ã¸å ¬é
ã¾ãã¯npmã®ãµã¤ãã«ã¦ã¼ã¶ã¼ç»é²ãããããã npm ã§ããã¾ãã
$ npm adduser yssk22 password [email protected] ... ...
å°ãããããã㨠~/.npmrc ã¨ãããã¡ã¤ã«ã«å¹³æã§ãã¹ã¯ã¼ããæ¸ããã¦ãã¾ã£ã¦ããã®ã§ãä¸å¿ chmod 600 ~/.npmrc ããã¦ããã¨ãããã¨ã
ã§ããã¨ã¯å®éã«ãã£ã¬ã¯ããªãpublishãã¾ãã
$ npm publish .
ãã㧠http://npm.mape.me/ ã®ãµã¤ãã« tgz ãã¢ãããã¼ãããã¾ããtgz ã®ãã¡ã¤ã«åã¯ããã£ã¬ã¯ããªå.tgz ã«ãªãã®ã§ãããããããã®ããã«ãããã±ã¼ã¸ã®ååã¨åããã£ã¬ã¯ããªåã§éçºããæ¹ãè¯ãããªãã¨å¾ã«ãªã£ã¦æ°ãã¤ãã¾ãããé©å½ã«ãã£ã¦ããã®ã§ãl.tgz ã¨ããæå³ä¸æãªããã±ã¼ã¸åã«ãªã£ã¦ãã¾ãã¾ããã¨ãã
å ¬éã§ãããã
$ npm install extractcontent
ã¨ãã£ã¦ããµã¤ãããã®ã¤ã³ã¹ãã¼ã«ãã§ãããã¨ã確èªãã¾ãããã
- /usr/local/lib/node/.npm/
- /usr/local/lib/node/
ã¨ãã«ãã¤ã³ã¹ãã¼ã«æ¸ã¿ã®ããã±ã¼ã¸ãã¯ãã£ã¦ããã®ã§ããããå ¨é¨æ¶ãã¦ãããããã¨ããããããã¾ãã
ã¨ããã§ãã¹ã
JSã®ãã¹ããä»ã¾ã§ JSpec ã¨ãããã¤ã使ã£ã¦ãããã§ãããnode.js ã¯ããããã¡ã½ãããéåæå®è¡ãªã®ã§ããã¹ããçµããåã«ã¡ã¤ã³ã¹ã¬ãããçµäºãã¡ããã¨ãããããªæãã§ãã¹ãããã«ããã®ã§ãã
nodeunit ã¨ããã¢ã¸ã¥ã¼ã«ããã£ãã®ã§ããã使ãã¨ããããã§ãããã¹ãã®çµäºç®æã test.end() ã¨æ示çã«ç¤ºããã¨ã§ããããééããã¾ã§ã¡ã¤ã³ã¹ã¬ãããå¾ æ©ãããã¨ãããããªæ§é ã§ãã
ã¡ãªã¿ã«ãå ¨é¨éåæã§ããã print ãããã°ã¯ããã¨ç¡åã«ãªãã®ã§æ³¨æãã¦ãã ãããéä¸ãã¹ãæ¸ãã¦ãããããªãã¨ããããã