ååã®èšäºã§ã¯ã Node.JSã§ãã€ãããWEBãããžã§ã¯ãã
äœããã¹ããããéçºããçµéšã«ã€ããŠè©±ãå§ããŸããã æåã®éšåã¯ã¬ãã¥ãŒã§ãæè¡ã®é·æãšçæãæããã«ããéçºäžã«çŽé¢ããªããã°ãªããªãåé¡ã«ã€ããŠèŠåããããšãè©Šã¿ãŸããã ãã®èšäºã§ã¯ãæè¡çãªè©³çŽ°ã«ã€ããŠèª¬æããŸãã
ãhabraeffectãã«é¢ããããã€ãã®èšè
æ£çŽãªãšãããã¡ã€ã³ããã«ãªã³ã¯ãããµã€ãã®åŽ©å£ãå®æçã«èŠ³å¯ããåŸããã£ãšæ·±å»ãªæ°åãç®ã«ããããšãæåŸ
ããŠããŸããã 以åã®èšäºã¯äž¡æ¹ãšãã¡ã€ã³ããŒãžã«ã¢ã¯ã»ã¹ããŸããã
æåã®èšäºã¯éããããã°ãI PRãã«ããã賌èªè
ã®ã¿ã«è¡šç€ºãããŠããŸãããããããã¡ã€ã«ããã°ãNode.JSãã®2çªç®ã®èšäºã¯ã³ã¡ã³ãã§ããªãé·ãè°è«ãåŒãèµ·ãããŸãããäž¡æ¹ã®èšäºãããµã€ãã«ã¢ã¯ã»ã¹ãã人ã®æ°ã¯ã»ãŒåãã§ããã åæ§ã«å°ããã
ãããã®æ°å€ã¯ãæ·±å»ãªè² è·ã«ã€ããŠèªãã«ã¯å°ããããŸãã 蚪åã®ããŒã¯æã«ãhtopã¯ããã次ã®åçã瀺ããŸããã
è² è·å¹³åã¯1ã«éããããšããããŸããããåã³0.3ã0.5ã«ãªããŸããã ããŒãžã¯ããã«äžããããŸããã å¹³åããŒãžçææéã圢æã®ããŒã¿ã¯memcached-15-20msã§ãã memcachedã«ããŒã¿ããªãå Žåãçææéã¯40ã100ããªç§ã«å¢å ããŸãããããã¯éåžžã«ãŸãã§ãã äžéšã®èšªåè
ã¯ãSiegeããã³abãŠãŒãã£ãªãã£ãšLoadImpactãµãŒãã¹ã䜿çšããŠãµã€ãããã¹ãããŸããã åœæãç§ã¯ãã¹ãŠã®ããŒãžãNginxã«ãã£ãŠé©åã«ãã£ãã·ã¥ããããããã®ãªã¯ãšã¹ãã¯Node.JSã«å±ããªããšç¢ºä¿¡ããŠããŸããã ããã¯ããã§ã¯ãªãããšãå€æããŸããã åŸã«ãããŒãžãã£ãã·ã³ã°ã劚ããã¢ãžã¥ãŒã«ã®1ã€ã®äžæ£ãªåäœãçºèŠããŸããïŒããã«ã€ããŠã¯åŸã§è©³ãã説æããŸãïŒã å®è³ªçã«ãã¹ãŠã®ãªã¯ãšã¹ãã¯Node.JSã«ãã£ãŠåŠçããããµã€ãã¯å®å®ããŠåäœããŸããã
æ®å¿µãªãããèšäºã®äž»é¡ãšãªã³ã¯ãèšå®ãããŠãããµã€ãã®äž»é¡ã«ãã£ãŠãããã©ãŒå¹æããã©ã®çšåºŠç°ãªããã¯ããããŸããã ãããããµã€ããåãïŒãŸãããŸãã¯2åïŒã®äººã
ããèœã¡ãŠããå Žåãåé¡ã¯ãã¯ãããžãŒã®éžæãšã¯ã»ã©é ããã®ã§ãã
ãã¹ããšåºåžããŒã¿ã«åºã¥ããŠãç§ã¯ãããžã§ã¯ããéåžžã«å®å®ããŠããã蚪åè
ã®æ¥æ¿ãªæµå
¥ãããå Žåã«èœã¡ãããšã¯ãªããšçµè«ä»ããŸããã
建ç¯
éãšãœãããŠã§ã¢
ãã®ãµã€ãã¯ãVPSã«ããããªæ©èœãåããŠããŸãïŒ
- 1200Mhzã®åšæ³¢æ°ãä¿èšŒããã1ã€ã®CPUã
- 1024Mb RAM;
- 25Gb HDDïŒãã®ãããžã§ã¯ãã§ã¯ããã®ã€ã³ãžã±ãŒã¿ãŒã¯ç¹å¥ãªåœ¹å²ãæãããŸããïŒã
ãµãŒããŒã¯Ubuntu Serverãå®è¡ããŠããŸãã ç§ã¯æ
£ããŠããŸãã圌女ãšäžç·ã«ä»äºãããã®ã䟿å©ãªã®ã§ã圌女ãéžã³ãŸããã
è¿œå ã®ãœãããŠã§ã¢ã¯æäœéã€ã³ã¹ããŒã«ãããŸãã ãã®å Žåã次ã®ãšããã§ãã
- Node.JS-ãµãŒããŒäžã®JavaScriptã¢ããªã±ãŒã·ã§ã³ã©ã³ã¿ã€ã ã
- MongoDB-NoSQL DBMS;
- Memcached-ãã£ãã·ã¥ããŒã¢ã³ã
- Nginx-ããã³ããšã³ããµãŒããŒã
ãœãããŠã§ã¢ã®ããŒãžã§ã³ãææ°ã«ä¿ã€ããã«ããŠããŸãã
æ§æ
ããã©ã«ãã§ã¯ãNode.JSã¯ã·ã³ã°ã«ã¹ã¬ããã§å®è¡ãããŸãããç¹ã«ãã«ãã³ã¢ããã»ããµã®å ŽåãããŸã䟿å©ã§ã¯ãªããæé©ã§ããããŸããã ã»ãšãã©ããã«ãã¢ãžã¥ãŒã«ã¯ããã€ãã®ããã»ã¹ïŒWeb Workerã®ããŸããŸãªå®è£
ïŒã䟿å©ã«èµ·åããããã«èŠããŸããã
æšæºã®Node.JS APIã§ãããè¡ãããšã¯é£ãããããŸããã§ããã ããŒãžã§ã³0.6.0ã®ãªãªãŒã¹ã§ãNode.JSã¯æ°ããã¢ãžã¥ãŒã«
Clusterãå°å
¥ããŸããã è€æ°ã®Node.JSããã»ã¹ãéå§ããã¿ã¹ã¯ã倧å¹
ã«ç°¡çŽ åããŸãã ãã®ã¢ãžã¥ãŒã«ã®APIã䜿çšãããšãnet / httpãµãŒããŒãå
±éã®TCPããŒãã䜿çšããããŒãããã»ã¹ãåå²ã§ããŸãã 芪ããã»ã¹ã¯åããã»ã¹ã管çã§ããŸãïŒåæ¢ãæ°ããããã»ã¹ã®éå§ãäºæããªãçµäºãžã®å¿çã åããã»ã¹ã¯ã芪ãšã¡ãã»ãŒãžã亀æã§ããŸãã
ã¯ã©ã¹ã¿ãŒã䜿çšãããšå¿
èŠãªæ°ã®ããã»ã¹ãéå§ããã®ã«äŸ¿å©ã§ããã«ãããããããç°ãªãTCPããŒãã§ããŒãã®2ã€ã®ã€ã³ã¹ã¿ã³ã¹ãå®è¡ããŸãã æŽæ°äžã®ããŠã³ã¿ã€ã ãåé¿ããããã«ãããè¡ããŸããããããªããšãåèµ·åäžïŒã¡ãªã¿ã«æ°ç§ããããããŸããïŒããŠãŒã¶ãŒã¯ãµã€ãã«ã¢ã¯ã»ã¹ã§ããªããªããŸãã ããŒãã®ã€ã³ã¹ã¿ã³ã¹éã§ãè² è·ã¯
HttpUpstreamModule Nginxã䜿çšããŠåæ£ãããŸãã åèµ·åäžã«ã€ã³ã¹ã¿ã³ã¹ã®1ã€ã䜿çšã§ããªããªããšã2çªç®ã®ã€ã³ã¹ã¿ã³ã¹ããã¹ãŠã®ãªã¯ãšã¹ããåŒãç¶ããŸãã
Nginxã¯ãèš±å¯ãããŠããªããŠãŒã¶ãŒã«å¯ŸããŠããµã€ãã®ãã¹ãŠã®ããŒãžãçæéïŒ1åéïŒãã£ãã·ã¥ãããããã«æ§æãããŠããŸãã ããã«ãããNode.JSã®è² è·ã倧å¹
ã«è»œæžãããšåæã«ãé¢é£ããã³ã³ãã³ããéåžžã«è¿
éã«è¡šç€ºã§ããŸãã èš±å¯ãŠãŒã¶ãŒã®å Žåããã£ãã·ã¥æéã¯3ç§ã«èšå®ãããŸãã ããã¯äžè¬ã®ãŠãŒã¶ãŒã«ã¯å®å
šã«èŠããŸããããèš±å¯Cookieãå«ãå€æ°ã®ãªã¯ãšã¹ãã§ãµã€ããããŒãããããšãã䟵å
¥è
ãé²ãããšãã§ããŸãã
ã¢ãžã¥ãŒã«
Node.JSã§ã¢ããªã±ãŒã·ã§ã³ãéçºããå Žåãç¹å®ã®ã¿ã¹ã¯ãå®è¡ããã¢ãžã¥ãŒã«ãéžæãããšããåé¡ããã°ãã°çºçããŸãã äžéšã®ã¿ã¹ã¯ã«ã¯ããã§ã«å®çžŸã®ãã人æ°ã®ããã¢ãžã¥ãŒã«ããããŸãããä»ã®ã¿ã¹ã¯ã«ã¯ãéžæããã®ãããå°é£ã§ãã ã¢ãžã¥ãŒã«ãéžæããã«ã¯ããªãã¶ãŒããŒã®æ°ããã©ãŒã¯ã®æ°ãããã³æåŸã®ã³ãããã®æ¥ä»ã«æ³šç®ããå¿
èŠããããŸãïŒä»ã¯GitHubã«ã€ããŠèª¬æããŠããŸãïŒã ãããã®ã€ã³ãžã±ãŒã¿ã«ããããããžã§ã¯ããçããŠãããã©ãããå€æã§ããŸãã æè¿ç»å Žã
ãNode ToolboxãµãŒãã¹ã¯ããã®ã¿ã¹ã¯ãéåžžã«å®¹æã«ããŸãã
ããã§ããããžã§ã¯ãã®éçºçšã«éžæããã¢ãžã¥ãŒã«ã«ã€ããŠèª¬æããŸãã
ã€ãªã
github.com/senchalabs/connectãã®ã¢ãžã¥ãŒã«ã¯
Node.JS httpãµãŒããŒã®ã¢ããªã³ã§ããããã®æ©èœã倧å¹
ã«æ¡åŒµããŸãã 圌ã¯ã«ãŒãã£ã³ã°ãCookieãµããŒããã»ãã·ã§ã³ãµããŒãããªã¯ãšã¹ãæ¬æã®è§£æãªã©ã®æ©èœãè¿œå ããŸãããããªãã§ã¯ãNode.JSã§ã®Webã¢ããªã±ãŒã·ã§ã³ã®éçºã¯æªå€¢ã«ãªãããã§ãã ã»ãšãã©ã®æ¥ç¶æ©èœã¯ãã©ã°ã€ã³ãšããŠå®è£
ãããŸãã ãŸããæšæºããã±ãŒãžã«ã¯å«ãŸããŠããªã
æ¥ç¶çšã®åæ§ã«äŸ¿å©ãª
ãã©ã°ã€ã³ãå€æ°ãããŸãã ç¬èªã®ãã©ã°ã€ã³ãéçºããŠäžè¶³ããŠããæ©èœãè¿œå ããããšãéåžžã«ç°¡åã§ãã
ãã®ã¢ãžã¥ãŒã«ã®äººæ°ãšæ¥éãªéçºã«ãããããããNginxãNode.JSããã®åçããã£ãã·ã¥ããããšã劚ããåé¡ãããã«ãããŸããã ããã©ã«ãã§ã¯ãNginxã®proxy_cacheãã£ã¬ã¯ãã£ãã¯ã次ã®ããããŒã®å°ãªããšã1ã€ãå«ãŸããŠããå Žåãããã¯ãšã³ãã®å¿çããã£ãã·ã¥ããŸããã
- ã¯ãããŒãèšå®ãã
- å€ããno-cacheãããno-storeãããprivateãããŸãã¯ãmax-ageãã§ãæ°å€ãŸãã¯ãŒã以å€ã®å€ãå«ãCache-Controlã
- éå»ã®æ¥ä»ã§å€±å¹ããŸãã
- X-Accel-ExpiresïŒ0ã
æ¥ç¶ã»ãã·ã§ã³ã§ã¯ããã¹ãŠã®å¿çã§Set-CookieããããŒãéä¿¡ãããããã«å®è£
ãããŸããã ããã¯ããã©ãŠã¶ã»ãã·ã§ã³ã®å¯¿åœãããé·ã寿åœãæã€ã»ãã·ã§ã³ããµããŒãããããã«è¡ãããŸããã PHPã§ã¯ãã»ãã·ã§ã³æéãç¹å®ã®å€ã«èšå®ãããšããŠãŒã¶ãŒããµã€ãã§ã¢ã¯ãã£ãã«ãªã£ãŠããå Žåã§ãããã®æéåŸã«çµäºããŸãã Connectã¯ç°ãªãããªã·ãŒã䜿çšããŸã-Cookieã¯ãã¹ãŠã®ãªã¯ãšã¹ãã§æŽæ°ããããã®æå¹æéã¯çŸåšã®ãªã¯ãšã¹ãããã«ãŠã³ãããå§ããŸãã ãŠãŒã¶ãŒãã¢ã¯ãã£ããªéãã»ãã·ã§ã³ã¯çµäºããŸããã PHPã®ã¢ãããŒãã¯ãç§ã«ã¯ãã£ãšæ¬åœã®ããã§ãã ã»ãã·ã§ã³ã¯ããŸã é·æã®ããŒã¿ã¹ãã¬ãŒãžãç®çãšããŠããŸããã ã³ãŒãã«é©åãªå€æŽãå ãããã«ãªã¯ãšã¹ããéä¿¡ããŸããã ããã«ã
çãè°è«ã®åŸïŒç§ã®è±èªã«åããŠã¯ãããŸããïŒã劥åæ¡ãèŠã€ãããŸãã-æéåãã»ãã·ã§ã³ãã€ã³ã¹ããŒã«ãããŠããªããããCookieã¯1åã ãéä¿¡ãããããã«ãªããŸããã æå¹æéãå³å¯ã«æå®ãããã»ãã·ã§ã³ã®å Žåããã®åé¡ã¯ãŸã 解決ãããŠããŸããã
connect-memcached
github.com/balor/connect-memcachedãã®ã¢ãžã¥ãŒã«ã¯ãæ¥ç¶çšã®ãã©ã°ã€ã³ã§ãã ã»ãã·ã§ã³ãmemcachedã«ä¿åããæ©èœãè¿œå ããŸãã è¿œå ã®ãã©ã°ã€ã³ããªãå Žåãconnectã¯1ã€ã®ããã»ã¹ã®ã¡ã¢ãªã«ã®ã¿ã»ãã·ã§ã³ãä¿åã§ããŸãã ããã¯æããã«æŠéæ¡ä»¶ã§ã®äœ¿çšã«ã¯ååã§ã¯ãªãããã察å¿ãããã©ã°ã€ã³ã¯ãã¹ãŠã®äžè¬çãªã¹ãã¬ãŒãžçšã«ãã§ã«éçºãããŠããŸãã
éåæ
github.com/caolan/asyncãã®ã¢ãžã¥ãŒã«ããªããã°ãNode.JSã®éåæã³ãŒããæžãã®ã¯ã¯ããã«å°é£ã«ãªããŸãã ãã®ã©ã€ãã©ãªã«ã¯ãéåæåŒã³åºããããžã£ã°ãªã³ã°ãããŠãäºãã«åã蟌ãŸããå€ãã®é¢æ°ã§ã³ãŒããèšããŸããããšãã§ããªãã¡ãœãããå«ãŸããŠããŸãã ããšãã°ãè€æ°ã®éåæåŒã³åºããèµ·åããããããå®äºããããã®ã¢ã¯ã·ã§ã³ãå®è¡ããã®ãã¯ããã«ç°¡åã«ãªããŸãã èªè»¢è»ã®ãããªãçºæãé¿ããããã«ããã®ã©ã€ãã©ãªã®ãã¹ãŠã®æ©èœã«ç²Ÿéããããšã匷ããå§ãããŸãã
node-oauth
github.com/ciaranj/node-oauthãã®ã¢ãžã¥ãŒã«ã¯OAuthããã³OAuth2ãããã³ã«ãå®è£
ããŸããããã«ããããããã®ãããã³ã«ããµããŒããããœãŒã·ã£ã«ãããã¯ãŒã¯ãä»ããŠãµã€ãã§ã®ãŠãŒã¶ãŒèªèšŒãéåžžã«ç°¡åã«æäŸã§ããŸãã
node-cron
github.com/ncb000gt/node-cronãã®ã¢ãžã¥ãŒã«ã®ååã¯ããèªèº«ãè¡šããŠããŸãã ã¹ã±ãžã¥ãŒã«ã«åŸã£ãŠã¿ã¹ã¯ãå®è¡ã§ããŸãã ã¹ã±ãžã¥ãŒã«ã®æ§æã¯ãLinuxã§èª°ããæ
£ããŠããcronãšéåžžã«äŒŒãŠããŸãããnode-cronãšã¯ç°ãªããsecond-secondã®èµ·åééããµããŒãããŠããŸãã ã€ãŸãã10ç§ããšãããã«ã¯1ç§ããšã«å®è¡ããããã«ã¡ãœãããæ§æã§ããŸãã 人æ°ã®è³ªåãã¡ã€ã³ããŒãžã«æã¡èŸŒãã§Twitterã«æçš¿ãããªã©ã®ã¿ã¹ã¯ã¯ããã®ã¢ãžã¥ãŒã«ã䜿çšããŠéå§ãããŸãã
ããŒããããã
github.com/jdub/node-twitterãã®ã¢ãžã¥ãŒã«ã¯ãã¢ããªã±ãŒã·ã§ã³ãšTwitter APIã®çžäºäœçšãå®è£
ããŸãã åäœããã«ã¯ãäžèšã®node-oauthã¢ãžã¥ãŒã«ã䜿çšããŸãã
node-mongodb-native
github.com/christkv/node-mongodb-nativeãã®ã¢ãžã¥ãŒã«ã¯ãNoSQL DBMS MongoDBãžã®ã€ã³ã¿ãŒãã§ãŒã¹ã§ãã 圌ã®ç«¶åä»ç€Ÿã®äžã§ã圌ã¯æé«ã®æŽç·Žãšè¿
éãªéçºã§éç«ã£ãŠããŸãã ããŒã¿ããŒã¹ïŒããŒã«ïŒãžã®è€æ°ã®æ¥ç¶ãéãããšã¯ãããã«ãµããŒããããŠãããããç¬èªã®æŸèæãæžãå¿
èŠã¯ãããŸããã ãã®ã¢ãžã¥ãŒã«ã«åºã¥ããŠãããªã䟿å©ãª
ORM MongooseãéçºãããŸããã
node-memcached
github.com/3rd-Eden/node-memcachedç§ã®æèŠã§ã¯ããããNode.JSã®memcachedã¢ã¯ã»ã¹ã€ã³ã¿ãŒãã§ã€ã¹ã§ãã è€æ°ã®memcachedãµãŒããŒãšãããã®éã®ããŒé
åžãããã³æ¥ç¶ããŒã«ããµããŒãããŸãã
http-get
github.com/SaltwaterC/http-getãã®ã¢ãžã¥ãŒã«ã¯ãHTTP / HTTPSçµç±ã§ãªã¢ãŒããªãœãŒã¹ã«ã¢ã¯ã»ã¹ããããã«èšèšãããŠããŸãã ããã«ããããœãŒã·ã£ã«ãããã¯ãŒã¯çµç±ã§ãµã€ãã«ãã°ã€ã³ããŠãããŠãŒã¶ãŒã®åçãããŠã³ããŒããããŸãã
sprintf
github.com/maritz/node-sprintfãã®ååã瀺ãããã«ãJavaScriptã§sprintfé¢æ°ãšvsprintfé¢æ°ãå®è£
ãããå°ãããŠãéåžžã«äŸ¿å©ãªã¢ãžã¥ãŒã«ã
daemon.node
github.com/indexzero/daemon.nodeãã®ã¢ãžã¥ãŒã«ã«ãããNode.JSã¢ããªã±ãŒã·ã§ã³ããããŒã¢ã³ãç°¡åã«äœæã§ããŸãã ããã«ãããã¢ããªã±ãŒã·ã§ã³ãã³ã³ãœãŒã«ããåãé¢ããåºåããã°ãã¡ã€ã«ã«ãªãã€ã¬ã¯ãããã®ã䟿å©ã§ãã
ç§ã®è²¢ç®
次ã®ã¢ãžã¥ãŒã«ã¯ããããžã§ã¯ãã®äœæ¥äžã«ç§ãéçºãããã®ã§ã å·çã®æç¹ã§ãç§ã¯ãããã®ä»£ããã«æ¢è£œã®ãœãªã¥ãŒã·ã§ã³ãèŠã€ããããšãã§ããŸããã§ããã ãããã®ã¢ãžã¥ãŒã«ã¯
ãGitHubããã³npm modulesãã£ã¬ã¯ããªã§
å
¬éãããŠããŸãã
ãã
github.com/baryshev/aopãã®ã¢ãžã¥ãŒã«ã¯ãŸã
AOPãã¿ãŒã³ãå®å
šã«å®è£
ãããšäž»åŒµããŠããŸããã çŸåšã¯ãå¿
èŠã«å¿ããŠåäœãå€æŽã§ããã¢ã¹ãã¯ãã§é¢æ°ãã©ããã§ããåäžã®ã¡ãœãããå«ãŸããŠããŸãã ãã®ææ³ã¯ãé¢æ°ã®çµæããã£ãã·ã¥ããããã«éåžžã«äŸ¿å©ã§ãã
ããšãã°ãããçš®ã®éåæé¢æ°ããããŸãã
var someAsyncFunction = function(num, callback) { var result = num * 2; callback(undefined, result); };
ãã®é¢æ°ã¯é »ç¹ã«åŒã³åºãããçµæããã£ãã·ã¥ããå¿
èŠããããŸãã éåžžã次ã®ããã«ãªããŸãã
var someAsyncFunction = function(num, callback) { var key = 'someModule' + '_' + 'someAsyncFunction' + '_' + num; cache.get(key, function(error, cachedResult) { if (error || !cachedResult) { var result = num * 2; callback(undefined, result); cache.set(key, result); } else { callback(undefined, cachedResult); } }); };
ãããžã§ã¯ãã«ã¯ããã®ãããªæ©èœãå€æ°ååšããå ŽåããããŸãã ã³ãŒãã¯å€§ããèšãã¿ãèªã¿ã«ãããªãã倧éã®ã³ããŒïŒããŒã¹ãã衚瀺ãããŸãã ãããŠãããã§aop.wrapã§ãåãããšãã§ããŸãã
var someAsyncFunction = function(num, callback) { var result = num * 2; callback(undefined, result); }; someAsyncFunction = aop.wrap(someAsyncFunction, someAsyncFunction, aspects.cache, 'someModule', 'someAsyncFunction');
ãããšã¯å¥ã«ãã¢ã¹ãã¯ãã©ã€ãã©ãªãäœæããããã«ãã£ãã·ã¥é¢æ°ãå®çŸ©ããŸãããã®é¢æ°ã¯ããã¹ãŠããã£ãã·ã¥ãã圹å²ãæãããŸãã
module.exports.cache = function(method, params, moduleName, functionName) { var that = this;
å¿
èŠã«å¿ããŠãæ©èœé¢ãå¢ããããšãã§ããŸãã 倧èŠæš¡ãããžã§ã¯ãã§ã¯ããã®ã¢ãããŒãã«ããã³ãŒãã®éã倧å¹
ã«ç¯çŽããããã¹ãŠã®ãšã³ãããŒãšã³ãæ©èœã1ãæã«ããŒã«ã©ã€ãºãããŸãã
å°æ¥çã«ã¯ãAOPãã¿ãŒã³ã®æ®ãã®æ©èœãå®è£
ããŠããã®ã©ã€ãã©ãªãæ¡åŒµããäºå®ã§ãã
圢
github.com/baryshev/formãã®ã¢ãžã¥ãŒã«ã®ã¿ã¹ã¯ã¯ãå
¥åããŒã¿ããã§ãã¯ããŠãã£ã«ã¿ãªã³ã°ããããšã§ãã ã»ãšãã©ã®å Žåããããã¯ãã©ãŒã ã§ãããå€éšAPIãªã©ããã®ãªã¯ãšã¹ãã«å¿ããŠåä¿¡ããããŒã¿ã§ããããŸãã ãã®ã¢ãžã¥ãŒã«ã«ã¯
ããŒãæ€èšŒã©ã€ãã©ãªãå«ãŸããŠããããã®æ©èœãå®å
šã«äœ¿çšã§ããŸãã
ãã®ã¢ãžã¥ãŒã«ã®åäœåçã¯æ¬¡ã®ãšããã§ããåãã©ãŒã ã¯ããã£ã«ã¿ãŒãæããããŠããäžé£ã®ãã£ãŒã«ãïŒãã£ãŒã«ãã®å€ã«åœ±é¿ãäžããé¢æ°ïŒããã³ããªããŒã¿ãŒïŒãã£ãŒã«ãã®å€ãæ¡ä»¶ã«é©åããŠãããã©ããããã§ãã¯ããé¢æ°ïŒã«ãã£ãŠèšè¿°ãããŸãã ããŒã¿ãåä¿¡ãããšããããã¯ããã»ã¹ãã©ãŒã ã¡ãœããã«æž¡ãããŸãã ã³ãŒã«ããã¯ã§ã¯ããšã©ãŒã®èª¬æïŒããŒã¿ããã©ãŒã ã®åºæºãæºãããŠããªãå ŽåïŒãŸãã¯ãã£ã«ã¿ãŒåŠçããããã£ãŒã«ãã»ãããå«ããªããžã§ã¯ããååŸããããã«äœ¿çšããæºåãã§ããŠããŸãã å°ããªäœ¿çšäŸïŒ
var fields = { text: [ form.filter(form.Filter.trim), form.validator(form.Validator.notEmpty, 'Empty text'), form.validator(form.Validator.len, 'Bad text length', 30, 1000) ], name: [ form.filter(form.Filter.trim), form.validator(form.Validator.notEmpty, 'Empty name') ] }; var textForm = form.create(fields); textForm.process({'text' : 'some short text', 'name': 'tester'}, function(error, data) { console.log(error); console.log(data); });
ãã®å Žåãããã¹ããã£ãŒã«ãã®ãšã©ãŒãBad text lengthãã衚瀺ãããŸãã éä¿¡ãããããã¹ãã®é·ãã¯30æåæªæºã§ãã
ãã£ã«ã¿ãŒãšããªããŒã¿ãŒã¯é çªã«å®è¡ããããããè¡æ«ã«å€ãã®ã¹ããŒã¹ãè¿œå ããŠãããšã©ãŒãçºçããŸãã ãã§ãã¯ããåã«ãããªã ãã£ã«ã¿ãŒã§ã¹ããŒã¹ãåé€ãããŸãã
node-validatorããŒãžã§ç¬èªã®ãã£ã«ã¿ãŒãšããªããŒã¿ãŒã®äœææ¹æ³ãèªãã ãããœãŒã¹ã³ãŒããåç
§ãããããããšãã§ããŸããå°æ¥ã®èšç»ã§ã¯ããã®ã¢ãžã¥ãŒã«ã®ç§»æ€ããã©ãŠã¶ãŒã§äœ¿çšã§ããããã«ãããã®æ©èœãååã«ææžåããŸãã
configjs
github.com/baryshev/configjsãã®ã¢ãžã¥ãŒã«ã¯ãã¢ããªã±ãŒã·ã§ã³ã®äŸ¿å©ãªæ§æãç®çãšããŠããŸãã æ§æã¯éåžžã®JSãã¡ã€ã«ã«ä¿åãããŸããããã«ãããæ§æäžã«JavaScriptã䜿çšã§ããããã«ãªãããã¡ã€ã«ã®è¿œå ã®è§£æãäžèŠã«ãªããŸãã ããŸããŸãªç°å¢ïŒéçºãå®çšŒåããã¹ããªã©ïŒçšã«ããã€ãã®è¿œå æ§æãäœæããŠãã¡ã€ã³æ§æãæ¡åŒµããã³/ãŸãã¯å€æŽã§ããŸãã
localejs
github.com/baryshev/localejsãã®ã¢ãžã¥ãŒã«ã¯configjsã«éåžžã«äŒŒãŠããŸãããç°ãªãèšèªã®æååãä¿åããŠå€èšèªããµããŒãããããã«èšèšãããŠããŸãã ãã®ã¢ãžã¥ãŒã«ã¯ãããã¹ããå€ãã¢ããªã±ãŒã·ã§ã³ã«ã¯ã»ãšãã©é©ããŠããŸããã ãã®å Žåã
GetTextã«äŒŒããœãªã¥ãŒã·ã§ã³ã䜿çšããæ¹ã䟿å©ã§ãã å¿
èŠãªãã±ãŒã«ãããŒãããæ段ã«å ããŠãã¢ãžã¥ãŒã«ã«ã¯ãã·ã¢èªãšè±èªããµããŒãããæ°åãåºåããæ©èœãå«ãŸããŠããŸãã
ãã
github.com/baryshev/hub/blob/master/lib/index.jsããããããã®ã¢ãžã¥ãŒã«ã¯Node.JSã®æå°ã¢ãžã¥ãŒã«ã§ãããšäž»åŒµã§ããŸãã ããã¯1è¡ã®ã¿ã§æ§æãããŠããŸãïŒmodule.exports = {}; ãã ããããããªããšãéçºã¯ã¯ããã«è€éã«ãªããŸãã ãã®ã¢ãžã¥ãŒã«ã¯ãã¢ããªã±ãŒã·ã§ã³ã®å®è¡äžã«ãªããžã§ã¯ããæ ŒçŽããããã®ã³ã³ããã§ãã Node.JSæ©èœã䜿çšããŸã-æ¥ç¶ããããšãã¢ãžã¥ãŒã«ã¯äžåºŠã ãåæåãããŸãã requireïŒ 'moduleName'ïŒãžã®ãã¹ãŠã®åŒã³åºãã¯ãã¢ããªã±ãŒã·ã§ã³ã«ããã€ãã£ãŠããæåã®èšåã§åæåãããã¢ãžã¥ãŒã«ã®åãã€ã³ã¹ã¿ã³ã¹ãžã®åç
§ãè¿ããŸãã å®éãã¢ããªã±ãŒã·ã§ã³ã®åéšåéã§ãªãœãŒã¹ãå
±æããããã®ã°ããŒãã«ã¹ããŒã¹ã®äœ¿çšã«åã£ãŠä»£ãããŸãã ãã®ãããªããŒãºã¯éåžžã«é »ç¹ã«çºçããŸãã äŸïŒDBMSæ¥ç¶ããŒã«ããã£ãã·ã¥æ¥ç¶ããŒã«ãããŒããããæ§æãžã®ãªã³ã¯ãããã³ãã±ãŒã«ã ãããã®ãªãœãŒã¹ã¯ã¢ããªã±ãŒã·ã§ã³ã®å€ãã®éšåã§å¿
èŠã§ãããããããžã®ã¢ã¯ã»ã¹ã¯ç°¡åã§ãªããã°ãªããŸããã ãªãœãŒã¹ãåæåããããšããããªããžã§ã¯ãã®ããããã£ã«å²ãåœãŠãããå°æ¥çã«ã¯ããããäºåã«æ¥ç¶ããããšã§ä»ã®ã¢ãžã¥ãŒã«ããã¢ã¯ã»ã¹ã§ããããã«ãªããŸãã
æ¥ç¶å¿ç
ãã®æ¥ç¶çšãã©ã°ã€ã³ã¯ãCookieãç°¡åã«æäœããæ©èœãè¿œå ãããŠãŒã¶ãŒãžã®å¿çãçæãããã³ãã¬ãŒããšã³ãžã³ãå«ã¿ãŸãã ç¬èªã®ãã³ãã¬ãŒããšã³ãžã³ãéçºããŸããã ããªãããŸããããŸããã åºç€ã¯
EJSãã³ãã¬ãŒããšã³ãžã³ããåãããŸããããæçµçã«ã¯ãåæ§ã®æ§æã§ã¯ãããŸãããç¬èªã®æ©èœãåããå®å
šã«ç°ãªã補åã§ããããšãå€æããŸããã ããããããã¯å¥ã®èšäºã«ãšã£ãŠå€§ããªãããã¯ã§ãã
æ®å¿µãªããããã®ã¢ãžã¥ãŒã«ã¯ãŸã å
¬éãããŠããŸããã é©åã«å®è¡ãããããã¹ãŠã®ãšã©ãŒããŸã ä¿®æ£ãããŠããããã§ã¯ãããŸããã å°ãã®ç©ºãæéãã§ã次第ãè¿ã
å®æãããŠå
¬éããäºå®ã§ãã
ã¢ããªã±ãŒã·ã§ã³æ§é
ã¢ããªã±ãŒã·ã§ã³ã¯ãã¬ãŒã ã¯ãŒã¯ã䜿çšããªããããNode.JSã§ã¢ããªã±ãŒã·ã§ã³ãèšè¿°ããäžè¬çãªã¹ã¿ã€ã«ãšåžžèãé€ãããã®æ§é ã¯ã«ãŒã«ã«é¢é£ä»ããããŠããŸããã ã¢ããªã±ãŒã·ã§ã³ã¯MVCã¢ãã«ã䜿çšããŸãã
server.jsèµ·åãã¡ã€ã«ã«ã¯ãhttpãµãŒããŒã®èµ·åãæ¥ç¶ã®æ§æãæ§æãšèšèªã®ããŒããMongoDBããã³Memcachedãšã®æ¥ç¶ã®ç¢ºç«ãã³ã³ãããŒã©ãŒã®æ¥ç¶ãããå
ã®ã¢ãžã¥ãŒã«éã§å
±æããå¿
èŠã®ãããªãœãŒã¹ãžã®ãªã³ã¯ã®èšå®ãªã©ãã¢ããªã±ãŒã·ã§ã³ã®äž»èŠãªãªãœãŒã¹ã®åæåãå«ãŸããŠããŸãã ããã§ã¯ãå¿
èŠãªæ°ã®ããã»ã¹ãåå²ãããŸãã ãã¹ã¿ãŒããã»ã¹ã§ã¯ãnode-cronãèµ·åãããã¹ã±ãžã¥ãŒã«ãããã¿ã¹ã¯ãå®è¡ãããŸããåããã»ã¹ã§ã¯ãhttp-serversãèµ·åãããŸãã
åã³ã³ãããŒã©ãŒã§ãconnectã¯URLããã³ãã©ãŒã¡ãœããã«æ¥ç¶ããŸãã åãªã¯ãšã¹ãã¯ãæ¥ç¶ã®åæåäžã«äœæãããäžé£ã®ã¡ãœãããééããŸãã äŸïŒ
var server = connect(); server.listen(port, hub.config.app.host); if (hub.config.app.profiler) server.use(connect.profiler()); server.use(connect.cookieParser()); server.use(connect.bodyParser()); server.use(connect.session({ store: new connectMemcached(hub.config.app.session.memcached), secret: hub.config.app.session.secret, key: hub.config.app.session.cookie_name, cookie: hub.config.app.session.cookie })); server.use(connect.query()); server.use(connect.router(function(router) { hub.router = router; }));
ããã¯ãèŠæ±ãåŠçããŠå¿çãçæããã¢ã«ãŽãªãºã ã«æ°ããåäœãéåžžã«ç°¡åã«è¿œå ã§ãã䟿å©ãªã¡ã«ããºã ã§ãã
ã³ã³ãããŒã©ãŒã¯ãå¿
èŠã«å¿ããŠãMongoDbãŸãã¯MemcachedããããŒã¿ãåãåãã¢ãã«ã®ã¡ãœãããåŒã³åºããŸãã å¿çã®ãã¹ãŠã®ããŒã¿ã®æºåãã§ãããšãã³ã³ãããŒã©ãŒã¯ãã³ãã¬ãŒããšã³ãžã³ã«ã³ãã³ããäžããŠããŒãžã圢æããçæãããhtmlããŠãŒã¶ãŒã«éä¿¡ããŸãã
ãããã«
Node.JSã§ã®WEBã¢ããªã±ãŒã·ã§ã³éçºã®ãããã¯ã¯éåžžã«å€§ãããèå³æ·±ããã®ã§ãã 2ã€ã®èšäºã§å®å
šã«è¿°ã¹ãããšã¯äžå¯èœã§ãã ã¯ããããã¯ããããå¿
èŠãããŸããã éçºã®åºæ¬ååã説æããèµ·ããããåé¡ãææããããšããŸããã ããã§ãããã¯ããå
¥åãããã®ã«ååãªã¯ãã§ããããããã°ã
Googleãš
GitHubãå©ãã«ãªããŸãã ãã®èšäºã§åŒçšããGitHubã®ã¢ãžã¥ãŒã«ããŒãžãžã®ãã¹ãŠã®ãªã³ã¯ã«ã¯ãã¢ãžã¥ãŒã«ã®ã€ã³ã¹ããŒã«ã«é¢ãã詳现ãªèª¬æãšãã®äœ¿çšäŸãå«ãŸããŠããŸãã
ãããèªãã ãã¹ãŠã®äººã«æè¬ããŸãã ã³ã¡ã³ãã§ãã£ãŒãããã¯ã質åãèãããšã«éåžžã«èå³ããããŸãã