What the frak is Node JS?




                                                  NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
Claudio Cicali

                                 @caludio

                            Front-end engineer at
                               SponsorPay


                                                NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
Installing NodeJS

        Download, unpack, make, make install

                             ... or use homebrew

                  (and yes! It works on Windows too)


                                                   NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
JavaScript is an interpreted language
            which means that you need an JavaScript
               interpreter to run your programs

                            We often refer to these beasts
                            as JavaScript Engines or
                              JavaScript Virtual Machine


                                                     NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
Every browser has its own VM

                             Firefox? Spidermonkey
                            Internet Explorer? Chakra
                                     Chrome? V8
                              Safari? JavaScriptCore
                                  Opera? Carakan

                             Also Rhino, stand alone
                                                  NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
2009: this guy had an idea

                            (he is Ryan Dahl, btw)

                                                  NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
Make no mistake

                        NodeJS is NOT a “one man project”

        It is backed by Joyent, which is a big player
                 in the cloud computing business
                      (has money and people)




                                                   NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
If you already can write JavaScript
            in the browser why can’t you write
           JavaScript on the server then?




                                       NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
js VMs are getting better and better

                 Get one of them, include some
                additional libraries and wrap it up
               into a powerful, directly executable
                       JavaScript platform


                                          NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
After the idea, the design:

                            Google V8 would have been the VM
                            (Tried using Spidermonkey, no luck)

                                 Monoprocess, no threads

      Completely event driven (asynchronous, non-blocking)

                              Focus on NETWORKING


                                                           NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
What is the “Node Standard Library”?

                      A set of ready-to-use tools which gives you

- a powerful HTTP(S) library (client and server in no time)
- DNS name resolution
- Crypto
- access to the file system
- URL manipulation
- ZLIB
- UDP datagrams

                              http://nodejs.org/docs/latest/api/
                                                             NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
Those tools can JavaScript only
        or mixed with some C or C++
            for performances reason



                              NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
Now we have a platform to write REAL programs
                   (i.e.: no sandboxing) in JavaScript

         The JavaScript itself - oh, joy - it’s the latest stable V8

                      AND NO COMPATIBILITY LAYER! (cries)




                                                                   except ...
                                                     NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
... except for Node JS itself


                      With every new major version, you might
                         loose compatibility at the API level
                             (the Node standard library)


                (this is really more of a youth problem, anyway)


                                                         NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
Node JS is fast, in theory

Low maintencance (small impact on resources)

                    Programs must not stress the CPU

                            Programming is not THAT easy

                              API are well documented
                                                      NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
The Node JS standard library
                     can be EXTENDED by the use of
                               modules

                        This is serious business:
                   the number of available modules is
                                 HUGE

                            (and the quality? Well...)
                                                    NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
MODULES in NodeJS use a well known
            pattern/paradigm/standard: CommonJS
            (which is well known thanks to... NodeJS)

                                  Writing a module is easy

                                      Maybe too much :)

                            (oh, and you’d write “addons” in C/C++ as well)


                                                                  NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
Take a look:

                    https://github.com/joyent/node/wiki/modules




                                                      NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
Module management is EASY with




                                   https://npmjs.org/

                No need to install it: comes with Node!
                           (Node >= 0.6)

          With brew you need to run npmjs.org/install.sh

                                                        NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
Need to access MySQL from your program?

                             npm install mysql




                                                   NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
Publishing your module to the world
                                    is freacking easy too
                               (no moderation, no approval...)

                                      npm publish




                                                         NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
Modules can be of any complexity


              Connect is a big “middleware” module
              which gives you cookies, sessions, logging,
                              caching...

       Express is a web framework built on top of
                       Connect

                                                      NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
What “using modules” means


        //  The  “fs”  module  is  ready  to  use
        var  fs  =  require(“fs”);

        fs.open(“somefile”,  “r”,  function(error)  {

            //  Do  something  with  the  file

        });




                                                    NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
claudioc$  npm  install  sqlite


        //  Once  installed  the  “sqlite”  module  is
        //  ready  to  use
        var  sqlite  =  require(“sqlite”);

        var  db  =  new  sqlite.Database();

        db.open(“my_db”,  function(error)  {

            //  Do  something  with  the  database

        });

                                                 NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
There are libraries which run in the browser
                   AND in NodeJS as modules (transparently)


                            “As of 3.5.0, YUI runs natively on
                            Node.js and comes with an official
                            npm package for easy installation.”




                                                        NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
What can NodeJS be used for then?
                    Network servers (HTTP, Proxies, messaging)

                                  API backends

                              Real time applications

                                  Streaming data

                              “One page” web sites

                               Command line tools

                                      Bots             NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
Let me introduce you a couple
         of things I’ve done with NodeJS




                                NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
antipad.com
                             Real time collaborative code editor

                                Built on the giants’ shoulders:

                                The ACE editor (awesome)

                            The ShareJS library (super awesome)

                                      NodeJS (lovely)



                                                              NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
jecho


                    “Poor man” command line remote debugger
                               for mobile devices



                            https://github.com/claudioc/jecho



                                                         NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
I want moar!

                     http://www.nodebeginner.org/
                        (highly recommended)

                            http://nodetoolbox.com

                             http://nodeguide.com
                                               NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
Follow me! @caludio
                                              NodeJS - © Claudio Cicali
Tuesday, December 4, 2012
One more thing

                      “The guys that are getting paid the big bucks
                      to deliver scalable solutions aren’t up at night
                      feverishly rewriting their systems in Node.
                      They’re doing what they’ve always done:
                      measuring, testing, benchmarking, thinking
                      hard, keeping up with the academic literature
                      that pertains to their problems.”

                                                       -- Alex Payne

                            http://al3x.net/2010/07/27/node.html
                                                            NodeJS - © Claudio Cicali
Tuesday, December 4, 2012

Introduction to NodeJS

  • 1.
    What the frakis Node JS? NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 2.
    Claudio Cicali @caludio Front-end engineer at SponsorPay NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 3.
    Installing NodeJS Download, unpack, make, make install ... or use homebrew (and yes! It works on Windows too) NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 4.
    JavaScript is aninterpreted language which means that you need an JavaScript interpreter to run your programs We often refer to these beasts as JavaScript Engines or JavaScript Virtual Machine NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 5.
    Every browser hasits own VM Firefox? Spidermonkey Internet Explorer? Chakra Chrome? V8 Safari? JavaScriptCore Opera? Carakan Also Rhino, stand alone NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 6.
    2009: this guyhad an idea (he is Ryan Dahl, btw) NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 7.
    Make no mistake NodeJS is NOT a “one man project” It is backed by Joyent, which is a big player in the cloud computing business (has money and people) NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 8.
    If you alreadycan write JavaScript in the browser why can’t you write JavaScript on the server then? NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 9.
    js VMs aregetting better and better Get one of them, include some additional libraries and wrap it up into a powerful, directly executable JavaScript platform NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 10.
    After the idea,the design: Google V8 would have been the VM (Tried using Spidermonkey, no luck) Monoprocess, no threads Completely event driven (asynchronous, non-blocking) Focus on NETWORKING NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 11.
    What is the“Node Standard Library”? A set of ready-to-use tools which gives you - a powerful HTTP(S) library (client and server in no time) - DNS name resolution - Crypto - access to the file system - URL manipulation - ZLIB - UDP datagrams http://nodejs.org/docs/latest/api/ NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 12.
    Those tools canJavaScript only or mixed with some C or C++ for performances reason NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 13.
    Now we havea platform to write REAL programs (i.e.: no sandboxing) in JavaScript The JavaScript itself - oh, joy - it’s the latest stable V8 AND NO COMPATIBILITY LAYER! (cries) except ... NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 14.
    ... except forNode JS itself With every new major version, you might loose compatibility at the API level (the Node standard library) (this is really more of a youth problem, anyway) NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 15.
    Node JS isfast, in theory Low maintencance (small impact on resources) Programs must not stress the CPU Programming is not THAT easy API are well documented NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 16.
    The Node JSstandard library can be EXTENDED by the use of modules This is serious business: the number of available modules is HUGE (and the quality? Well...) NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 17.
    MODULES in NodeJSuse a well known pattern/paradigm/standard: CommonJS (which is well known thanks to... NodeJS) Writing a module is easy Maybe too much :) (oh, and you’d write “addons” in C/C++ as well) NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 18.
    Take a look: https://github.com/joyent/node/wiki/modules NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 19.
    Module management isEASY with https://npmjs.org/ No need to install it: comes with Node! (Node >= 0.6) With brew you need to run npmjs.org/install.sh NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 20.
    Need to accessMySQL from your program? npm install mysql NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 21.
    Publishing your moduleto the world is freacking easy too (no moderation, no approval...) npm publish NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 22.
    Modules can beof any complexity Connect is a big “middleware” module which gives you cookies, sessions, logging, caching... Express is a web framework built on top of Connect NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 23.
    What “using modules”means //  The  “fs”  module  is  ready  to  use var  fs  =  require(“fs”); fs.open(“somefile”,  “r”,  function(error)  { //  Do  something  with  the  file }); NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 24.
    claudioc$  npm  install sqlite //  Once  installed  the  “sqlite”  module  is //  ready  to  use var  sqlite  =  require(“sqlite”); var  db  =  new  sqlite.Database(); db.open(“my_db”,  function(error)  { //  Do  something  with  the  database }); NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 25.
    There are librarieswhich run in the browser AND in NodeJS as modules (transparently) “As of 3.5.0, YUI runs natively on Node.js and comes with an official npm package for easy installation.” NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 26.
    What can NodeJSbe used for then? Network servers (HTTP, Proxies, messaging) API backends Real time applications Streaming data “One page” web sites Command line tools Bots NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 27.
    NodeJS - ©Claudio Cicali Tuesday, December 4, 2012
  • 28.
    Let me introduceyou a couple of things I’ve done with NodeJS NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 29.
    antipad.com Real time collaborative code editor Built on the giants’ shoulders: The ACE editor (awesome) The ShareJS library (super awesome) NodeJS (lovely) NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 30.
    jecho “Poor man” command line remote debugger for mobile devices https://github.com/claudioc/jecho NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 31.
    I want moar! http://www.nodebeginner.org/ (highly recommended) http://nodetoolbox.com http://nodeguide.com NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 32.
    Follow me! @caludio NodeJS - © Claudio Cicali Tuesday, December 4, 2012
  • 33.
    One more thing “The guys that are getting paid the big bucks to deliver scalable solutions aren’t up at night feverishly rewriting their systems in Node. They’re doing what they’ve always done: measuring, testing, benchmarking, thinking hard, keeping up with the academic literature that pertains to their problems.” -- Alex Payne http://al3x.net/2010/07/27/node.html NodeJS - © Claudio Cicali Tuesday, December 4, 2012