Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Build Your Application On Node.js

Build Your Application On Node.js

mahdi pedramrazi

March 21, 2014
Tweet

More Decks by mahdi pedramrazi

Other Decks in Programming

Transcript

  1. Agenda   •  What  is  Node.js   •  Hello  world

     with  Node.js   •  Node  modules   •  Callback  Hell  vs  Modular  Code   •  Promises    
  2. What  is  Node.js   •  PlaDorm  built  on  Chrome's  JavaScript

     run.me  (V8   JavaScript  engine)   •  Building  fast,  scalable  Internet  applica.ons.   •  Event-­‐driven,  non-­‐blocking  I/O  model     •  Lightweight  and  efficient   •   8000  lines  of  C/C++,  2000  lines  of  Javascript   •  14  contributors.  (  2009  )  
  3. Event  Loop   •  Single  threaded   •  Doesn’t  spend

     .me  wai.ng  for  things  to  finish     •  Sequen.ally  execute  a  number  of  tasks  very   rapidly  
  4. Hello  World   •  Download  Node.js  from  h`p://nodejs.org   • 

    Install  Node.js   •  Open  your  Terminal   •  Type  “node”   •  Type  “console.log(‘Hello  World’);  
  5. How  to  Load  Modules.   //  Node  Built  In  modules

      var  h`p  =  require('h`p');     //custom  modules   var  customModule  =  require('path/to/your/module');  
  6. Node  Modules   h`p://nodejs.org/api/   35+  Build  In  Modules  

      require(“h`p”);   require(“path”);   require(“process”);   require(“os”);      
  7. How  to  load  Public  Modules   NPM  (  Node  Packaged

     Modules  )     npmjs.org     In  the  root  folder  run:  “npm  install”     loads  all  dependencies  from  package.json     //updates  package.json   Npm  install  –save  moment    
  8. Asynchronous  Model  quiz   var  ac.on_one  =  RPS.getEvents(func.on(){    console.log(arguments);

      });     var  ac.on_two  =  RPS.getEvent(“test”,  func.on(){    console.log(arguments);   });   var  Ac.on_three  =  require('events').EventEmi`er;  
  9. Issac  Schlueter  (NPM  Developer  )   Write  small  modules  that

     each  do   one  thing,  and  assemble  them  into   other  modules  that  do  a  bigger   thing.     You  can't  get  into  callback  hell  if   you  don't  go  there.  
  10. CommonJS   •  Modern  JavaScript  Design  pa`ern.   •  CommonJS

     module  is  a  reusable  piece  of   JavaScript  which  exports  specific  objects  made   available  to  any  dependent  code.     •  Unlike  AMD,  there  are  typically  no  func.on   wrappers  around  such  modules  (so  we  won't   see  define  here  for  example).  
  11. Paradigm  of  Doom   the  situa.on  where  code  marches  to

     the  right   faster  than  it  marches  forward.  
  12. Promises     Represents  a  proxy  for  a  value  which

     is   not  known  at  it’s  crea.on  .me.  
  13. Promise  Libraries   •  Q     •  When  

    •  RQ  (  Doug  Crockford  )   •  Deffered   •  Async  
  14. Se`led   •  Use  allSe`led  If  you  want  to  wait

     for  all  of  the   promises  to  either  be  fulfilled  or  rejected.  
  15. Useful  Links   Express  -­‐  Web  development  framework  for  

    Node.js   Tips,  Libraries,  Tutorials   Q.Js   Node  Inspector