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

Intro to Node.js

Andrew
June 06, 2013

Intro to Node.js

Introduction to programming with Node.js.

Andrew

June 06, 2013
Tweet

Other Decks in Programming

Transcript

  1. ABOUT ME Father Coder Wanna be Ultra Runner and Principal

    Consultant @ Coding Inertia www.codinginertia.com
  2. WHAT IS NODE.JS “ Node.js uses an event-driven, non-blocking I/O

    model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices. ”
  3. SIMPLE WEB SERVER v a r h t t p

    = r e q u i r e ( ' h t t p ' ) ; h t t p . c r e a t e S e r v e r ( f u n c t i o n ( r e q , r e s ) { r e s . w r i t e H e a d ( 2 0 0 , { ' C o n t e n t - T y p e ' : ' t e x t / p l a i n ' } ) ; r e s . e n d ( ' H e l l o W o r l d \ n ' ) ; } ) . l i s t e n ( 9 9 9 5 , ' 1 2 7 . 0 . 0 . 1 ' ) ; c o n s o l e . l o g ( ' S e r v e r r u n n i n g a t h t t p : / / 1 2 7 . 0 . 0 . 1 : 9 9 9 5 / ' ) ; $ n o d e s i m p l e w e b s e r v e r . j s http://127.0.0.1:9995
  4. CORE MODULES v a r h t t p =

    r e q u i r e ( ' h t t p ' ) ; h t t p . c r e a t e S e r v e r ( f u n c t i o n ( r e q , r e s ) { r e s . w r i t e H e a d ( 2 0 0 , { ' C o n t e n t - T y p e ' : ' t e x t / p l a i n ' } ) ; r e s . e n d ( ' H e l l o W o r l d \ n ' ) ; } ) . l i s t e n ( 9 9 9 5 , ' 1 2 7 . 0 . 0 . 1 ' ) ; c o n s o l e . l o g ( ' S e r v e r r u n n i n g a t h t t p : / / 1 2 7 . 0 . 0 . 1 : 9 9 9 5 / ' ) ; http://nodejs.org/api
  5. NPM $ n p m i n s t a

    l l u n d e r s c o r e v a r _ = r e q u i r e ( ' u n d e r s c o r e ' ) ; _ ( [ 1 , 2 , 3 ] ) . e a c h ( f u n c t i o n ( n u m ) { c o n s o l e . l o g ( n u m ) ; } ) ; $ n o d e u d e r s c o r e . j s
  6. FILE MODULES ...folders may also be modules / / c

    i r c l e . j s v a r P I = M a t h . P I ; e x p o r t s . a r e a = f u n c t i o n ( r ) { r e t u r n P I * r * r ; } ; e x p o r t s . c i r c u m f e r e n c e = f u n c t i o n ( r ) { r e t u r n 2 * P I * r ; } ; / / f o o . j s v a r c i r c l e = r e q u i r e ( ' . / c i r c l e . j s ' ) ; c o n s o l e . l o g ( ' T h e a r e a o f a c i r c l e o f r a d i u s 4 i s ' + c i r c l e . a r e a ( 4 ) ) ; $ n o d e f o o . j s
  7. FOLDER MODULES / / . / s q u a

    r e / i n d e x . j s e x p o r t s . a r e a = f u n c t i o n ( h ) { r e t u r n h * h ; } ; / / f o o . j s v a r s q u a r e = r e q u i r e ( ' . / s q u a r e . j s ' ) ; c o n s o l e . l o g ( ' T h e a r e a o f a s q u a r e o f r a d i u s 4 i s ' + s q u a r e . a r e a ( 4 ) ) ; $ n o d e f o o . j s
  8. SOCKET.IO $ n p m i n s t a

    l l s o c k e t . i o v a r a p p = r e q u i r e ( ' h t t p ' ) . c r e a t e S e r v e r ( h a n d l e r ) , i o = r e q u i r e ( ' s o c k e t . i o ' ) . l i s t e n ( a p p ) , f s = r e q u i r e ( ' f s ' ) a p p . l i s t e n ( 9 5 9 5 ) ; f u n c t i o n h a n d l e r ( r e q , r e s ) { f s . r e a d F i l e ( _ _ d i r n a m e + ' / i n d e x . h t m l ' , f u n c t i o n ( e r r , d a t a ) { i f ( e r r ) { r e s . w r i t e H e a d ( 5 0 0 ) ; r e t u r n r e s . e n d ( ' E r r o r l o a d i n g i n d e x . h t m l ' ) ; } r e s . w r i t e H e a d ( 2 0 0 ) ; r e s . e n d ( d a t a ) ; } ) ; } http://socket.io
  9. SOCKET.IO ...app.js continued i o . s o c k

    e t s . o n ( ' c o n n e c t i o n ' , f u n c t i o n ( s o c k e t ) { s o c k e t . e m i t ( ' n e w s ' , { m e s s a g e : ' w o r l d ' } ) ; s o c k e t . o n ( ' m y o t h e r e v e n t ' , f u n c t i o n ( d a t a ) { c o n s o l e . l o g ( d a t a ) ; / / s o c k e t . e m i t ( ' n e w s ' , d a t a ) ; i o . s o c k e t s . e m i t ( ' n e w s ' , d a t a ) ; } ) ; } ) ;
  10. SOCKET.IO index.html . . . < s c r i

    p t s r c = " / s o c k e t . i o / s o c k e t . i o . j s " > < / s c r i p t > < s c r i p t > < / s c r i p t > < / h e a d > < b o d y > < i n p u t t y p e = " t e x t " i d = " m y T e x t " > < / i n p u t > < b u t t o n > S e n d < / b u t t o n > < d i v i d = " r e s p o n s e " > < / d i v > < / b o d y > v a r s o c k e t = i o . c o n n e c t ( ' h t t p : / / l o c a l h o s t : 9 5 9 5 ' ) ; s o c k e t . o n ( ' n e w s ' , f u n c t i o n ( d a t a ) { c o n s o l e . l o g ( d a t a ) ; $ ( " # r e s p o n s e " ) . a p p e n d ( " < d i v > " + d a t a . m e s s a g e + " < / d i v > " ) ; } ) ; $ ( f u n c t i o n ( ) { $ ( " b u t t o n " ) . c l i c k ( f u n c t i o n ( ) { s o c k e t . e m i t ( ' m y o t h e r e v e n t ' , { m e s s a g e : $ ( " # m y T e x t " ) . v a l ( ) } ) ; } ) ; } ) ;
  11. DEBUGGING built in debugger better still use and debug in

    Chrome! v a r s q u a r e = r e q u i r e ( ' . / s q u a r e ' ) ; d e b u g g e r ; c o n s o l e . l o g ( ' s q u a r e o f 2 : ' + s q u a r e . a r e a ( 2 ) ) ; $ n o d e d e b u g d e b u g g e r - b a s i c . j s node-inspector
  12. NODE-INSPECTOR / / a p p . j s v

    a r h t t p = r e q u i r e ( ' h t t p ' ) ; h t t p . c r e a t e S e r v e r ( f u n c t i o n ( r e q , r e s ) { r e s . w r i t e H e a d ( 2 0 0 , { ' C o n t e n t - T y p e ' : ' t e x t / p l a i n ' } ) ; r e s . e n d ( ' H e l l o W o r l d \ n ' ) ; } ) . l i s t e n ( 9 9 9 5 , ' 1 2 7 . 0 . 0 . 1 ' ) ; c o n s o l e . l o g ( ' S e r v e r r u n n i n g a t h t t p : / / 1 2 7 . 0 . 0 . 1 : 9 9 9 5 / ' ) ; $ n p m i n s t a l l - g n o d e - i n s p e c t o r $ n o d e - - d e b u g a p p . j s $ n o d e - i n s p e c t o r & http://127.0.0.1:8080/debug?port=5858
  13. EXPRESS WEB SERVER minimalist web framework for node $ n

    p m i n s t a l l e x p r e s s v a r e x p r e s s = r e q u i r e ( ' e x p r e s s ' ) ; v a r a p p = e x p r e s s ( ) ; a p p . g e t ( ' / ' , f u n c t i o n ( r e q , r e s ) { r e s . s e n d ( ' H e l l o W o r l d f r o m E x p r e s s ' ) ; } ) ; a p p . l i s t e n ( 3 0 0 0 ) ;
  14. EXPRESS - BOOTSTRAP $ n p m i n s

    t a l l - g e x p r e s s $ e x p r e s s d e m o $ c d d e m o $ n p m i n s t a l l $ n o d e a p p