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

All aboard the NodeJS Express

All aboard the NodeJS Express

Get on board the NodeJS Express as we take a journey through what makes NodeJS special. Server-side JavaScript that has an event loop for a heart, we'll delve into its single threaded nature and the advantages provided. From there we'll pass through the land of the Node Package Management tool, how to set up your own package and bring in useful 3rd party packages as dependencies. Our final destination is ExpressJS, a Sinatra inspired framework for NodeJS.

David Boyer

June 06, 2014
Tweet

More Decks by David Boyer

Other Decks in Programming

Transcript

  1. The Details JavaScript Google's V8 engine Open Source - MIT

    License Cross platform Single threaded code Multi threaded IO
  2. What can it do? Build web applications Creating command line

    tools Create desktop applications (node-webkit)
  3. Action!* n o d e c o n s o

    l e . l o g ( ' H e l l o S c o t c h ' ) ;
  4. Using the API v a r f s = r

    e q u i r e ( ' f s ' ) ; / / F i l e s y s t e m A P I v a r h t t p = r e q u i r e ( ' h t t p ' ) ; / / h t t p A P I v a r c r y p t o = r e q u i r e ( ' c r y p t o ' ) ; / / C r y p t o A P I
  5. What does the code look like? v a r f

    s = r e q u i r e ( ' f s ' ) ; v a r r a w D a t a = f s . r e a d F i l e S y n c ( ' s o t r . j s o n ' ) ; v a r d a t a = J S O N . p a r s e ( r a w D a t a ) ; c o n s o l e . l o g ( n e w D a t e ( ) ) ; c o n s o l e . l o g ( d a t a . m e s s a g e ) ;
  6. Why not just be multi- threaded? Each thread needs more

    memory Switching between threads costs CPU Avoids thread safety issues
  7. v a r c l u s t e r

    = r e q u i r e ( ' c l u s t e r ' ) ; v a r h t t p = r e q u i r e ( ' h t t p ' ) ; v a r c p u s = r e q u i r e ( ' o s ' ) . c p u s ( ) . l e n g t h ; i f ( c l u s t e r . i s M a s t e r ) { f o r ( v a r i = 0 ; i < c p u s ; i + + ) { c l u s t e r . f o r k ( ) ; } c l u s t e r . o n ( ' e x i t ' , f u n c t i o n ( w o r k e r , c o d e , s i g n a l c o n s o l e . l o g ( ' P r o c e s s I D ' + w o r k e r . p r o c e s s . p i d } ) ; } e l s e { 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 ) { . . . } ) . l i s t }
  8. Highway to Hell f s . r e a d

    F i l e ( ' s o t r . j s o n ' , f u n c t i o n ( e r r , d a t a ) { v a r i n f o = J S O N . p a r s e ( d a t a ) ; d b . f i n d O n e ( { i d : i n f o . i d } , f u n c t i o n ( e r r , r e c o r d ) f s . w r i t e F i l e ( ' s o t r . d a t ' , r e c o r d . t i t l e , f u n c t i o c o n s o l e . l o g ( ' F i n i s h j o b ' ) ; } ) } ) } )
  9. Escape from Nested Callbacks Break it apart Use a async

    module like "async" Promises Generators (ES6)
  10. Node as a web application v a r h t

    t p = r e q u i r e ( ' h t t p ' ) ; v a r s e r v e r = 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 . e n d ( ' H e l l o S c o t c h ! ' ) ; } ) ; s e r v e r . l i s t e n ( 8 0 ) ;
  11. Modules / / c o n f . j s

    f u n c t i o n C o n f e r e n c e ( n a m e , y e a r ) { t h i s . n a m e = n a m e ; t h i s . y e a r = y e a r ; } ; C o n f e r e n c e . p r o t o t y p e . g e t T i t l e = f u n c t i o n ( ) { r e t u r n t h i s . n a m e + ' ' + t h i s . y e a r ; } ; m o d u l e . e x p o r t s = C o n f e r e n c e / / i n d e x . j s v a r C o n f = r e q u i r e ( _ _ d i r n a m e + ' / c o n f . j s ' ) ; v a r s o t r = n e w C o n f ( ' S c o t c h o n t h e R o c k s ' , 2 0 1 4 ) ; c o n s o l e . l o g ( s o t r . g e t T i t l e ( ) ) ;
  12. Initialise your project n p m i n i t

    n a m e : ( c o n f e r e n c e ) s o t r - e x a m p l e v e r s i o n : ( 0 . 0 . 0 ) 0 . 1 . 2 d e s c r i p t i o n : A n e x a m p l e p r o j e c t e n t r y p o i n t : ( i n d e x . j s ) i n d e x . j s t e s t c o m m a n d : g r u n t n o d e u n i t g i t r e p o s i t o r y : g i t : / / g i t h u b . c o m / m i s t e r d a i / i s n o t a . k e y w o r d s : e x a m p l e , a p p a u t h o r : D a v i d B o y e r l i c e n s e : ( B S D - 2 - C l a u s e ) M I T
  13. Common modules Databases: mysql, mongodb, sqlite Callbacks: async, q, when,

    co Testing: Mocha, nodeunit, should Templating: Jade, handlebars, ejs Web frameworks: express,
  14. Installing modules n p m i n s t a

    l l a s y n c n p m i n s t a l l - - s a v e c o f f e e - s c r i p t n p m i n s t a l l - - s a v e - d e v g r u n t - i m a g e m i n n p m i n s t a l l n p m u n i n s t a l l - - s a v e c o f f e e - s c r i p t
  15. Installing commands n p m i n s t a

    l l - g c o f f e e - s c r i p t n p m u n i n s t a l l - g g r u n t - c l i n p m i n s t a l l - g g u l p
  16. Other npm super powers Publish modules Execute scripts (e.g. npm

    build, npm run) Bump your version number (semver)
  17. Summary Let npm manage your modules Use it to install

    useful tools such as grunt Don't have to keep node_modules in source control Store project related commands in package.json
  18. Version 4 Bundled middleware are now their own modules*. *

    Except "static". More robust routing. a p p . r o u t e r ( ) is gone and more flexible methods are now available.
  19. Installing m k d i r w e b s

    i t e c d w e b s i t e n p m i n i t n p m i n s t a l l - - s a v e e x p r e s s t o u c h i n d e x . j s
  20. Try being MEAN!  Popular stack for building Node.js based

    web apps. MongoDB E xpress AngularJS Node.js