SlideShare uma empresa Scribd logo
1 de 19
Baixar para ler offline
Node.js  on  Cloud  Foundry
      〜~NPMによるNode.jsモジュール管理理のサポートについて〜~  
 オリジナルコンテンツ:Cloud Foundry Supports Node.js Modules with NPM
http://blog.cloudfoundry.com/2012/05/24/cloud-foundry-supports-node-js-modules-with-npm/	




                                                                                             2012/7/26
                                                                       ヴイエムウェア株式会社
                                                                                                 池⽥田俊彦




                                                                                              © 2011 VMware Inc. All rights reserved
⾃自⼰己紹介




                       Toshihiko Ikeda!
                     tikeda@vmware.com /
                         @tikeda123!




    12 years in enterprise middleware(java application server)!
                              /SOA!
                   c/c++/Java/Ruby/JavaScript!
              Spring Framework(core)/rails/grails?!

2
Node.jsとは?


    1.  ブラウザGoogle  Chrome⽤用に開発されたJavaScriptエンジンV8をサ
        ーバランタイム環境で動作させるので、⼿手軽に開発できるJavaScript
        でコーディングできる。
    2.  ノンブロッキングI/O、イベントドリブンなアーキテクチャモデルの
        採⽤用しているので、軽量量でスケーラブルなアプリケーションを構築で
        きる。




      Chrome     TCP/HTTP/      Async         Event
                                                             Other         JS
        V8         DNS           I/O         Support
                                                          C/C++  libcv   Modules
      C++  lib   C/C++  lib   C/C++  lib   C/C++  libcv

                       C/C++  Environment                                JS  Env
                              Node.js  Under-‐‑‒the-‐‑‒hood



3
意外とI/Oコストばかにならない!?




      http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/




4
Multi-threaded server - Threads are spawned for every connection



       User1                        i/o request
                                                                 T1
                                                                                               DB

       User2                        i/o request                  T2
                                                                                    Blocking
                                                                                       I/O
      User3
Refreshes 2 times
                                                                 T3   T4 T5


                                                                 T6   T7 T8                    FS
      User4
refreshes 3 times
                                                                               T9


                                                                  Multi threaded
                                                                      server
       T   Thread
                    Because every I/o is blocking, server
                    spawns a thread per connection* to support
                    multiple requests



   5
Non-blocking & Evented I/O (Node.js server)

                                                                          JS                            C/C++

                                                                               T1
        User1                            i/o request                           V8                                         DB
                                                                 T1                                 Libio
                                                                                 T1                 POSIX
                                                                 V8
                                                                      Event loop V8                 Async
        User2                            i/o request                  (Libev)                      Threads

                                                                 T1            delegate i/o to                  Non-blocking
                                                                                                           t2       I/O
                                                                 V8                libeio          t1
      User3
Refreshes 2 times                                                     Single                               t4
                                                                      thread                       t3
                                                                      serves        T1                    t6
                                                                      all users     V8             t5
      User4
refreshes 3 times                                                T1                                        t7             FS
                                                                 V8
                                                                                         i/o result returned
                                                                 T1                 T1
                                                                                          2 EL after x time
                                                                 V8       T1        V8
   T1         JS Thread running                                           V8
   V8         your code (Single threaded)
                                                                                    Node.js

   t1            POSIX threads doing
         t2
                 async I/O (multi-threaded)
  t3                                           Everything except your (JS) code is runs in parallel (by libio)

   6
Multi-‐‑‒threaded  blocking  server  (Apache)  
                                                VS  Event-‐‑‒driven,  non-‐‑‒blocking  server  (Nginx)  performance




                                     Reqs/sec v/s concurrent connections

                         At ~4000 concurrent connections,
                         - Nginx can serve ~9000 reqs/sec
                         - Apache can serve ~3000 reqs/sec

                                 Ref: http://blog.webfaction.com/a-little-holiday-present




 7
NPMによるNode.jsモジュール管理理のサポートについて

                  NPM(Node  Package  Manager)とは
    Node.js⽤用のパッケージ管理理(Rubyで⾔言えば、RVMに相当)で、nodeアプリケー
    ションのインストール時、パッケージの依存関係を管理理しながら、適切切なバージョ
                   ンのモジュールをインストールする。



•  npm  install
    package.jsonに記述されたパッケージのバージョン情報をもとに、各モジュールの依存
    関係を抽出し、必要なパッケージを./node_̲modulesディレクトリ内にインストール。

•  npm  list
    パッケージの依存関係を表⽰示。

•  npm  shrinkwrap
    インストールされる各パッケージの依存関係に従いパッケージバージョンをロックダウ
    ンする。ロックダウンされたバージョンによるパッケージの依存関係をnpm-‐‑‒
    shrinkwrap.jsonに記述する。



8
NPMによるNode.jsモジュール管理理のサポート

          NPM(node  package  manager)でNodeモジュールの依存関係を管理理

    各モジュールの依存関係の問題より、パッケージのインストールに失敗するケースがある。
    $  vmc  push  app-‐‑‒name
      …
    Installing  dependencies.  Node  version  0.6.8
    Failed  getting  the  requested  package:  bcrypt@0.4.5


    package.jsonに記述されたパッケージのバージョン情報をもとに、必要なパッケージを./
    node_̲modulesディレクトリ内にインストール。
    $  npm  install
      …

     package.jsonに記述されたパッケージのバージョン情報をもとに、各モジュールの依存関係
     を抽出し、npm-‐‑‒shrinkwrap.jsonファイルに記述する。cfへのアプリケーション展開時
     (vmc  push)にnpm-‐‑‒shrinkwrap.jsonファイルの情報を送り、依存関係の問題を発⽣生させ
     ないようにする。
     $  npm  shrinkwrap
       …


9
NPMによるNode.jsモジュール管理理のサポート
 cloudfoundry.jsonファイルに、以下の内容のように、ignoreNodeModulesのパラメータに  “true”  
 を設定する。これがないとnode_̲modulesディレクトリ以下のモジュールをアップロードすること
 になる。bcrypt,fibers,nowjsなどのNativeライブラリを使った場合、実⾏行行環境でコンパイルの
 必要があるためモジュールをアップロードできない。このcloudfoundry.jsonの設定は必須
        cloudfoundry.jsonファイル	

     { "ignoreNodeModules" : true }


     vmc  pushコマンドによるアプリケーションの展開
     vmc  push  –runtime=node06(Node.jsバージョン指定が可能)
     $vmc  push  –runtime=node06
     Would  you  like  to  deploy  from  the  current  directory?  [Yn]:  Y
     Application  Name:  ike-‐‑‒node
     Detected  a  Node.js  Application,  is  this  correct?  [Yn]:  Y
     Application  Deployed  URL  [ike-‐‑‒node.cloudfoundry.com]:  
     ………………
     Creating  Application:  OK
     Uploading  Application:
         Checking  for  available  resources:  OK
         Processing  resources:  OK
         Packing  application:  OK
         Uploading  (15K):  OK      
     Push  Status:  OK
     Staging  Application  'ike-‐‑‒node':  OK                                                                                            
     Starting  Application  'ike-‐‑‒node':  OK  




10
まとめ

1.  JavaScriptエンジンV8をランタイム環境で動作しているので、⼿手軽
     に開発できるJavaScriptでコーディングできる。


2.  ノンブロッキングI/O、イベントドリブンなアーキテクチャモデルの
     採⽤用しているので、軽量量でスケーラブルなアプリケーションを構築
     できる。


3.  Cloudfoundry.comでNPMをサポート、Nodeモジュールの依存関係を
     NPMにて管理理が可能になった。


4.  NPMのバージョン管理理により、bcrypt,fibers,nowjsなどのNativeラ
     イブラリ利利⽤用時におけるバージョンの⾮非互換性問題を解決	




11
NPMによるNode.jsモジュール管理理のサポート  制限事項	

§  今のcf.comでは、npm-‐‑‒shrinkwrap.jsonに記述しているバージョンのかわりに
  gitリポジトリのURLを指定することができません。

{
    "name":  "node-‐‑‒dependencies-‐‑‒example",
    "version":  "0.1.0",
    "dependencies":  {
        "express":  {
            "version":  ”3.0.0rc1",
            "dependencies":  {
                "connect":  {
                    "version":  "1.8.7",
……
}

                                                  NG	
{
    "name":  "node-‐‑‒dependencies-‐‑‒example",
    "version":  "0.1.0",
    "dependencies":  {
        "express":  {
            "version":  ”3.0.0rc1",
            “from”:  “git://github.com/visionmedia/express.git”,
            "dependencies":  {
                "connect":  {
                    "version":  "1.8.7",
……
}

13
最も使われている開発⾔言語は?

     「Python」や「Perl」などの⾔言語を抜き「JavaScript」が8位に上昇しています。




       TIOBE  Softwareが、2012年年3⽉月の⼈人気プログラミング⾔言語ランキング
14
node.js  +  expressのアプリケーションをデプロイ
 1.任意のディレクトリでexpressコマンドを実⾏行行し、プロジェクトを⾃自動作成する。
 $express  -‐‑‒t  ejs  examplehello
       create  :  examplehello
       ……
       create  :  examplehello/views/index.ejs

           dont  forget  to  install  dependencies:
           $  cd  examplehello  &&  npm  install



     2.expressコマンドで作成したモジュールを確認
     $cd  examplehello
     $ls
     app.js    package.json                public     routes   views

      app.js:expressで出⼒力力されたメインのJavaScriptプログラム
      package.json:expressフレームワークで必要なモジュールとそのバージョンが記述されている。
      public:Web  ui系で利利⽤用されるイメージファイル、CSS,JavaScriptが保存されているフォルダ
      routes:
      view:




15
第8回  Cloud  Foundry輪輪読会会場




16
node.js  +  expressのアプリケーションをデプロイ	

3.npm  installコマンドで必要なnodeモジュールをダウンロードする。
$npm  install
……
express@2.5.11  node_̲modules/express
├──  qs@0.4.2
├──  mime@1.2.4
├──  mkdirp@0.3.0
└──  connect@1.9.2  (formidable@1.0.11)




17
NPMによるNode.jsモジュール管理理のサポートについて
 app.js
	
var app = require("express").createServer();	
var bcrypt = require("bcrypt");	
 	
app.get("/", function(req, res) {	
  var salt = bcrypt.genSaltSync(10);	
  var hash = bcrypt.hashSync("B4c0//", salt);	
  if (bcrypt.compareSync("B4c0//", hash))	
    res.send("World is safe!");	
  else	
    res.send("World is in trouble!");	
});	
var port = process.env.VCAP_APP_PORT || 3000;	
app.listen(port);    		

package.json

{
    "name"  :  "node-‐‑‒dependencies-‐‑‒example",
    "version"  :  "0.1.0",
    "dependencies"  :  {
        "express"  :  "2.5.x",
        "bcrypt"  :  "0.5.x"
    }
}
18
NPMによるNode.jsモジュール管理理のサポートについて

 npm-‐‑‒shrinkwrap.json
{
    "name":  "node-‐‑‒dependencies-‐‑‒example",
    "version":  "0.1.0",
    "dependencies":  {
        "express":  {
            "version":  "2.5.9",
            "dependencies":  {
                "connect":  {
                    "version":  "1.8.7",
                    "dependencies":  {
                        "formidable":  {
                            "version":  "1.0.9"
                        }
                    }
                },
                "mime":  {
                    "version":  "1.2.4"
                },
                "qs":  {
                    "version":  "0.4.2"
                },
                "mkdirp":  {
                    "version":  "0.3.0"
                }
            }
        },
        "bcrypt":  {
            "version":  "0.5.0"
        }
    }
}


  19

Mais conteúdo relacionado

Destaque

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destaque (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

第8回CloudFoundry輪読会発表資料(Node.js on Cloud Foundry)

  • 1. Node.js  on  Cloud  Foundry 〜~NPMによるNode.jsモジュール管理理のサポートについて〜~   オリジナルコンテンツ:Cloud Foundry Supports Node.js Modules with NPM http://blog.cloudfoundry.com/2012/05/24/cloud-foundry-supports-node-js-modules-with-npm/ 2012/7/26 ヴイエムウェア株式会社 池⽥田俊彦 © 2011 VMware Inc. All rights reserved
  • 2. ⾃自⼰己紹介 Toshihiko Ikeda! tikeda@vmware.com / @tikeda123! 12 years in enterprise middleware(java application server)! /SOA! c/c++/Java/Ruby/JavaScript! Spring Framework(core)/rails/grails?! 2
  • 3. Node.jsとは? 1.  ブラウザGoogle  Chrome⽤用に開発されたJavaScriptエンジンV8をサ ーバランタイム環境で動作させるので、⼿手軽に開発できるJavaScript でコーディングできる。 2.  ノンブロッキングI/O、イベントドリブンなアーキテクチャモデルの 採⽤用しているので、軽量量でスケーラブルなアプリケーションを構築で きる。 Chrome TCP/HTTP/ Async Event Other JS V8 DNS I/O Support C/C++  libcv Modules C++  lib C/C++  lib C/C++  lib C/C++  libcv C/C++  Environment JS  Env Node.js  Under-‐‑‒the-‐‑‒hood 3
  • 4. 意外とI/Oコストばかにならない!? http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/ 4
  • 5. Multi-threaded server - Threads are spawned for every connection User1 i/o request T1 DB User2 i/o request T2 Blocking I/O User3 Refreshes 2 times T3 T4 T5 T6 T7 T8 FS User4 refreshes 3 times T9 Multi threaded server T Thread Because every I/o is blocking, server spawns a thread per connection* to support multiple requests 5
  • 6. Non-blocking & Evented I/O (Node.js server) JS C/C++ T1 User1 i/o request V8 DB T1 Libio T1 POSIX V8 Event loop V8 Async User2 i/o request (Libev) Threads T1 delegate i/o to Non-blocking t2 I/O V8 libeio t1 User3 Refreshes 2 times Single t4 thread t3 serves T1 t6 all users V8 t5 User4 refreshes 3 times T1 t7 FS V8 i/o result returned T1 T1 2 EL after x time V8 T1 V8 T1 JS Thread running V8 V8 your code (Single threaded) Node.js t1 POSIX threads doing t2 async I/O (multi-threaded) t3 Everything except your (JS) code is runs in parallel (by libio) 6
  • 7. Multi-‐‑‒threaded  blocking  server  (Apache)                                                  VS  Event-‐‑‒driven,  non-‐‑‒blocking  server  (Nginx)  performance Reqs/sec v/s concurrent connections At ~4000 concurrent connections, - Nginx can serve ~9000 reqs/sec - Apache can serve ~3000 reqs/sec Ref: http://blog.webfaction.com/a-little-holiday-present 7
  • 8. NPMによるNode.jsモジュール管理理のサポートについて NPM(Node  Package  Manager)とは Node.js⽤用のパッケージ管理理(Rubyで⾔言えば、RVMに相当)で、nodeアプリケー ションのインストール時、パッケージの依存関係を管理理しながら、適切切なバージョ ンのモジュールをインストールする。 •  npm  install package.jsonに記述されたパッケージのバージョン情報をもとに、各モジュールの依存 関係を抽出し、必要なパッケージを./node_̲modulesディレクトリ内にインストール。 •  npm  list パッケージの依存関係を表⽰示。 •  npm  shrinkwrap インストールされる各パッケージの依存関係に従いパッケージバージョンをロックダウ ンする。ロックダウンされたバージョンによるパッケージの依存関係をnpm-‐‑‒ shrinkwrap.jsonに記述する。 8
  • 9. NPMによるNode.jsモジュール管理理のサポート NPM(node  package  manager)でNodeモジュールの依存関係を管理理 各モジュールの依存関係の問題より、パッケージのインストールに失敗するケースがある。 $  vmc  push  app-‐‑‒name  … Installing  dependencies.  Node  version  0.6.8 Failed  getting  the  requested  package:  bcrypt@0.4.5 package.jsonに記述されたパッケージのバージョン情報をもとに、必要なパッケージを./ node_̲modulesディレクトリ内にインストール。 $  npm  install  … package.jsonに記述されたパッケージのバージョン情報をもとに、各モジュールの依存関係 を抽出し、npm-‐‑‒shrinkwrap.jsonファイルに記述する。cfへのアプリケーション展開時 (vmc  push)にnpm-‐‑‒shrinkwrap.jsonファイルの情報を送り、依存関係の問題を発⽣生させ ないようにする。 $  npm  shrinkwrap  … 9
  • 10. NPMによるNode.jsモジュール管理理のサポート cloudfoundry.jsonファイルに、以下の内容のように、ignoreNodeModulesのパラメータに  “true”   を設定する。これがないとnode_̲modulesディレクトリ以下のモジュールをアップロードすること になる。bcrypt,fibers,nowjsなどのNativeライブラリを使った場合、実⾏行行環境でコンパイルの 必要があるためモジュールをアップロードできない。このcloudfoundry.jsonの設定は必須 cloudfoundry.jsonファイル { "ignoreNodeModules" : true } vmc  pushコマンドによるアプリケーションの展開 vmc  push  –runtime=node06(Node.jsバージョン指定が可能) $vmc  push  –runtime=node06 Would  you  like  to  deploy  from  the  current  directory?  [Yn]:  Y Application  Name:  ike-‐‑‒node Detected  a  Node.js  Application,  is  this  correct?  [Yn]:  Y Application  Deployed  URL  [ike-‐‑‒node.cloudfoundry.com]:   ……………… Creating  Application:  OK Uploading  Application:    Checking  for  available  resources:  OK    Processing  resources:  OK    Packing  application:  OK    Uploading  (15K):  OK       Push  Status:  OK Staging  Application  'ike-‐‑‒node':  OK                                                                                             Starting  Application  'ike-‐‑‒node':  OK   10
  • 11. まとめ 1.  JavaScriptエンジンV8をランタイム環境で動作しているので、⼿手軽 に開発できるJavaScriptでコーディングできる。 2.  ノンブロッキングI/O、イベントドリブンなアーキテクチャモデルの 採⽤用しているので、軽量量でスケーラブルなアプリケーションを構築 できる。 3.  Cloudfoundry.comでNPMをサポート、Nodeモジュールの依存関係を NPMにて管理理が可能になった。 4.  NPMのバージョン管理理により、bcrypt,fibers,nowjsなどのNativeラ イブラリ利利⽤用時におけるバージョンの⾮非互換性問題を解決 11
  • 12.
  • 13. NPMによるNode.jsモジュール管理理のサポート  制限事項 §  今のcf.comでは、npm-‐‑‒shrinkwrap.jsonに記述しているバージョンのかわりに gitリポジトリのURLを指定することができません。 {    "name":  "node-‐‑‒dependencies-‐‑‒example",    "version":  "0.1.0",    "dependencies":  {        "express":  {            "version":  ”3.0.0rc1",            "dependencies":  {                "connect":  {                    "version":  "1.8.7", …… } NG {    "name":  "node-‐‑‒dependencies-‐‑‒example",    "version":  "0.1.0",    "dependencies":  {        "express":  {            "version":  ”3.0.0rc1",            “from”:  “git://github.com/visionmedia/express.git”,            "dependencies":  {                "connect":  {                    "version":  "1.8.7", …… } 13
  • 14. 最も使われている開発⾔言語は? 「Python」や「Perl」などの⾔言語を抜き「JavaScript」が8位に上昇しています。 TIOBE  Softwareが、2012年年3⽉月の⼈人気プログラミング⾔言語ランキング 14
  • 15. node.js  +  expressのアプリケーションをデプロイ 1.任意のディレクトリでexpressコマンドを実⾏行行し、プロジェクトを⾃自動作成する。 $express  -‐‑‒t  ejs  examplehello      create  :  examplehello      ……      create  :  examplehello/views/index.ejs      dont  forget  to  install  dependencies:      $  cd  examplehello  &&  npm  install 2.expressコマンドで作成したモジュールを確認 $cd  examplehello $ls app.js package.json public routes views app.js:expressで出⼒力力されたメインのJavaScriptプログラム package.json:expressフレームワークで必要なモジュールとそのバージョンが記述されている。 public:Web  ui系で利利⽤用されるイメージファイル、CSS,JavaScriptが保存されているフォルダ routes: view: 15
  • 17. node.js  +  expressのアプリケーションをデプロイ 3.npm  installコマンドで必要なnodeモジュールをダウンロードする。 $npm  install …… express@2.5.11  node_̲modules/express ├──  qs@0.4.2 ├──  mime@1.2.4 ├──  mkdirp@0.3.0 └──  connect@1.9.2  (formidable@1.0.11) 17
  • 18. NPMによるNode.jsモジュール管理理のサポートについて app.js var app = require("express").createServer(); var bcrypt = require("bcrypt");   app.get("/", function(req, res) {   var salt = bcrypt.genSaltSync(10);   var hash = bcrypt.hashSync("B4c0//", salt);   if (bcrypt.compareSync("B4c0//", hash))     res.send("World is safe!");   else     res.send("World is in trouble!"); }); var port = process.env.VCAP_APP_PORT || 3000; app.listen(port); package.json {    "name"  :  "node-‐‑‒dependencies-‐‑‒example",    "version"  :  "0.1.0",    "dependencies"  :  {        "express"  :  "2.5.x",        "bcrypt"  :  "0.5.x"    } } 18
  • 19. NPMによるNode.jsモジュール管理理のサポートについて npm-‐‑‒shrinkwrap.json {    "name":  "node-‐‑‒dependencies-‐‑‒example",    "version":  "0.1.0",    "dependencies":  {        "express":  {            "version":  "2.5.9",            "dependencies":  {                "connect":  {                    "version":  "1.8.7",                    "dependencies":  {                        "formidable":  {                            "version":  "1.0.9"                        }                    }                },                "mime":  {                    "version":  "1.2.4"                },                "qs":  {                    "version":  "0.4.2"                },                "mkdirp":  {                    "version":  "0.3.0"                }            }        },        "bcrypt":  {            "version":  "0.5.0"        }    } } 19