introduction to node.js

Post on 17-May-2015

939 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

A quick talk I did at London Titanium meet up introducting Node.js

TRANSCRIPT

Introduction to Node.js

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

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)

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.

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/))

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/

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/

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'););

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););

});});

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'););

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

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

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

About to hit V3About to hit V3

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)

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

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.

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/

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

top related