Download - Introduction to Node.js
![Page 1: Introduction to Node.js](https://reader038.vdocuments.site/reader038/viewer/2022103114/5557d474d8b42ab6258b490d/html5/thumbnails/1.jpg)
Introduction to Node.js
![Page 2: Introduction to Node.js](https://reader038.vdocuments.site/reader038/viewer/2022103114/5557d474d8b42ab6258b490d/html5/thumbnails/2.jpg)
WhoWhoDeveloper for Kainos (Java, Scala, Rails, JavaScript, HTML,Developer for Kainos (Java, Scala, Rails, JavaScript, HTML,CSS)CSS)
Student at University of BathStudent at University of Bath
Write a lot about JavaScript atWrite a lot about JavaScript atwww.javascriptplayground.comwww.javascriptplayground.com
@Jack_Franklin on Twitter@Jack_Franklin on Twitter
![Page 3: Introduction to Node.js](https://reader038.vdocuments.site/reader038/viewer/2022103114/5557d474d8b42ab6258b490d/html5/thumbnails/3.jpg)
Origins of NodeOrigins of NodeServer side JavaScript done rightServer side JavaScript done right
runs on Chrome's V8 Engine (it's quick)runs on Chrome's V8 Engine (it's quick)
Evented I/O - runs single non-blocking thread with eventEvented I/O - runs single non-blocking thread with eventlooploop
this is great as JS was designed to be run in a single threadthis is great as JS was designed to be run in a single threadenvironment (browser)environment (browser)
![Page 4: Introduction to Node.js](https://reader038.vdocuments.site/reader038/viewer/2022103114/5557d474d8b42ab6258b490d/html5/thumbnails/4.jpg)
Node right nowNode right nowV0.8 standardised the API (non breaking)V0.8 standardised the API (non breaking)
currently V0.8.4, much more stable than < 0.8currently V0.8.4, much more stable than < 0.8
Install via installers, from source or via package manager likeInstall via installers, from source or via package manager likeBrew.Brew.
![Page 5: Introduction to Node.js](https://reader038.vdocuments.site/reader038/viewer/2022103114/5557d474d8b42ab6258b490d/html5/thumbnails/5.jpg)
NPMNPMPackage Manager for Node (think rubygems / bundler forPackage Manager for Node (think rubygems / bundler forNode)Node)
Full of really useful modules (and some less useful ones)Full of really useful modules (and some less useful ones)
12,627 packages as of yesterday (browse at12,627 packages as of yesterday (browse athttp://search.npmjs.org/http://search.npmjs.org/))
![Page 6: Introduction to Node.js](https://reader038.vdocuments.site/reader038/viewer/2022103114/5557d474d8b42ab6258b490d/html5/thumbnails/6.jpg)
Event Driven JavaScriptEvent Driven JavaScriptLearn to think Asynchronously Learn to think Asynchronously "Once node has completed a"Once node has completed atask, the callback for it is fired. But there can only be onetask, the callback for it is fired. But there can only be onecallback firing at the same time. Until that callback hascallback firing at the same time. Until that callback hasfinished executing, all other callbacks have to wait in line. Infinished executing, all other callbacks have to wait in line. Inaddition to that, there is no guarantee on the order in whichaddition to that, there is no guarantee on the order in whichthe callbacks will fire."the callbacks will fire." From: From: http://debuggable.com/postshttp://debuggable.com/posts/understanding-node-js:4bd98440-45e4-4a9a-/understanding-node-js:4bd98440-45e4-4a9a-8ef7-0f7ecbdd56cb8ef7-0f7ecbdd56cb
Related: Async.js Related: Async.js https://github.com/caolan/async/https://github.com/caolan/async/
![Page 7: Introduction to Node.js](https://reader038.vdocuments.site/reader038/viewer/2022103114/5557d474d8b42ab6258b490d/html5/thumbnails/7.jpg)
Callbacks CallbacksLots and lots of callbacksLots and lots of callbacks
varvar netnet == requirerequire(('net''net'););
varvar serverserver == netnet..createServercreateServer((functionfunction ((socketsocket)) {{
socketsocket..writewrite(('Echo server\r\n''Echo server\r\n'););
socketsocket..pipepipe((socketsocket););
});});
serverserver..listenlisten((13371337,, '127.0.0.1''127.0.0.1'););
How to avoid: How to avoid: http://callbackhell.com/http://callbackhell.com/
![Page 8: Introduction to Node.js](https://reader038.vdocuments.site/reader038/viewer/2022103114/5557d474d8b42ab6258b490d/html5/thumbnails/8.jpg)
Naming your CallbacksNaming your CallbacksDid you know you can name them?Did you know you can name them?
varvar netnet == requirerequire(('net''net'););
varvar serverserver == netnet..createServercreateServer((functionfunction writeResponsewriteResponse((socketsocket)) {{
socketsocket..writewrite(('Echo server\r\n''Echo server\r\n'););
socketsocket..pipepipe((socketsocket););
});});
serverserver..listenlisten((13371337,, '127.0.0.1''127.0.0.1'););
![Page 9: Introduction to Node.js](https://reader038.vdocuments.site/reader038/viewer/2022103114/5557d474d8b42ab6258b490d/html5/thumbnails/9.jpg)
Avoiding Callbacks: ModulesAvoiding Callbacks: Modules"Write small modules that each do one thing, and assemble"Write small modules that each do one thing, and assemblethem into other modules that do a bigger thing. You can't getthem into other modules that do a bigger thing. You can't getinto callback hell if you don't go there."into callback hell if you don't go there." Isaac Schlueter - Isaac Schlueter -Node.js core contributor @izsNode.js core contributor @izs
write your code in a file like normal:write your code in a file like normal:
//file socket-module.js//file socket-module.js
functionfunction writeResponsewriteResponse((socketsocket)) {{
socketsocket..writewrite(('Echo server\r\n''Echo server\r\n'););
socketsocket..pipepipe((socketsocket););
});});
![Page 10: Introduction to Node.js](https://reader038.vdocuments.site/reader038/viewer/2022103114/5557d474d8b42ab6258b490d/html5/thumbnails/10.jpg)
modulemodule..exportsexports == {{
respresp:: writeResponsewriteResponse
}}
(this follows the CommonJS module structure)(this follows the CommonJS module structure)
require and use itrequire and use it
varvar netnet == requirerequire(('net''net'););
varvar socketModulesocketModule == requirerequire(('socket-module''socket-module'););
varvar serverserver == netnet..createServercreateServer((socketModulesocketModule..respresp););
serverserver..listenlisten((13371337,, '127.0.0.1''127.0.0.1'););
![Page 11: Introduction to Node.js](https://reader038.vdocuments.site/reader038/viewer/2022103114/5557d474d8b42ab6258b490d/html5/thumbnails/11.jpg)
Databases with NodeDatabases with NodeRedis & the Redis NPM Package is awesomeRedis & the Redis NPM Package is awesome
Adapters for all common DB solutionsAdapters for all common DB solutions
![Page 12: Introduction to Node.js](https://reader038.vdocuments.site/reader038/viewer/2022103114/5557d474d8b42ab6258b490d/html5/thumbnails/12.jpg)
Web FrameworksWeb FrameworksMost popular is Express JS - Most popular is Express JS - www.expressjs.comwww.expressjs.com
Loads out there - Google "node js framework"Loads out there - Google "node js framework"
Geddy, Flatiron, RailwayJSGeddy, Flatiron, RailwayJS
Tools like this are slowly but surely maturingTools like this are slowly but surely maturing
![Page 13: Introduction to Node.js](https://reader038.vdocuments.site/reader038/viewer/2022103114/5557d474d8b42ab6258b490d/html5/thumbnails/13.jpg)
ExpressExpressvarvar appapp == expressexpress..createServercreateServer();();
appapp..getget(('/''/',, functionfunction((reqreq,, resres){){
resres..sendsend(('Hello World''Hello World'););
});});
appapp..listenlisten((30003000););
routing, views (Jade), etcrouting, views (Jade), etc
very extensiblevery extensible
![Page 14: Introduction to Node.js](https://reader038.vdocuments.site/reader038/viewer/2022103114/5557d474d8b42ab6258b490d/html5/thumbnails/14.jpg)
About to hit V3About to hit V3
![Page 15: Introduction to Node.js](https://reader038.vdocuments.site/reader038/viewer/2022103114/5557d474d8b42ab6258b490d/html5/thumbnails/15.jpg)
Unit TestingUnit TestingNodeUnit NodeUnit https://github.com/caolan/nodeunithttps://github.com/caolan/nodeunit
Mocha Mocha http://visionmedia.github.com/mocha/http://visionmedia.github.com/mocha/
Lots more. Find one that suits (I love Mocha)Lots more. Find one that suits (I love Mocha)
![Page 16: Introduction to Node.js](https://reader038.vdocuments.site/reader038/viewer/2022103114/5557d474d8b42ab6258b490d/html5/thumbnails/16.jpg)
In the wildIn the wildJSBin by @rem - JSBin by @rem - www.jsbin.comwww.jsbin.com - pure awesomeness - pure awesomeness
TweetDig by @mheap - TweetDig by @mheap - www.tweetdig.comwww.tweetdig.com - 2.5m tweets per - 2.5m tweets perdayday
![Page 17: Introduction to Node.js](https://reader038.vdocuments.site/reader038/viewer/2022103114/5557d474d8b42ab6258b490d/html5/thumbnails/17.jpg)
To Sum UpTo Sum UpNode is still very young, although standards and conventionsNode is still very young, although standards and conventionsare beginning to be defined.are beginning to be defined.
Lack of resources is slowly becoming less of an issue.Lack of resources is slowly becoming less of an issue.
V0.8 is huge improvement on prior versions.V0.8 is huge improvement on prior versions.
Node is seriously quick if used properly.Node is seriously quick if used properly.
![Page 18: Introduction to Node.js](https://reader038.vdocuments.site/reader038/viewer/2022103114/5557d474d8b42ab6258b490d/html5/thumbnails/18.jpg)
Further ResourcesFurther Resources@Peepcode screencasts @Peepcode screencasts www.peepcode.comwww.peepcode.com
CodeSchool Node course CodeSchool Node course www.codeschool.comwww.codeschool.com
Async JavaScript book from Trevor BurnhanAsync JavaScript book from Trevor Burnhanwww.leanpub.com/asyncjswww.leanpub.com/asyncjs
How to Node blog How to Node blog www.howtonode.org/www.howtonode.org/
![Page 19: Introduction to Node.js](https://reader038.vdocuments.site/reader038/viewer/2022103114/5557d474d8b42ab6258b490d/html5/thumbnails/19.jpg)
Any Questions?Any Questions?Slides on Github: gist.github.com/jackfranklinSlides on Github: gist.github.com/jackfranklin
www.javascriptplayground.comwww.javascriptplayground.com for JavaScript tutorials for JavaScript tutorials(including Node)(including Node)
@Jack_Franklin if you can put up with even more of me@Jack_Franklin if you can put up with even more of meramblingrambling
![Page 20: Introduction to Node.js](https://reader038.vdocuments.site/reader038/viewer/2022103114/5557d474d8b42ab6258b490d/html5/thumbnails/20.jpg)