Your SlideShare is downloading. ×
0
Introduction to Node.js: What, why and how?    Christian Joudrey - @cjoudrey
“Nodes goal is to provide an easy way to buildscalable network programs.”
What is node?• Command line tool to run JavaScript:  $ node my_app.js• Built on top of Googles V8 JavaScript engine• Provi...
my_app.js:  setTimeout(function () {    console.log(world);  }, 2000);  console.log(hello);Running it:  $ node my_app.js  ...
The cost of I/O?• Accessing RAM               ~ 250 CPU cycles• Disk operations            ~ 41 000 000 CPU cycles  o Read...
• I/O is expensive• Most web applications are I/O bound, not CPU bound
Handling I/O• Apache is multithreaded (depending on conf.)   o Spawns a thread per request (or process)• Each request is i...
Thread-per-connection is expensivehttp://blog.webfaction.com/a-little-holiday-present
• nginx uses an event-loop, like node.
Enter node...• Single thread for your code• ...but, I/O runs in parallel• Handle thousands of concurrent connections with ...
I/O in node• No function should directly perform I/O.• To receive info from disk, network, or another process there  must ...
CommonJS Module Systemmymodule.js:module.exports.add = function (a, b) {   return a + b;};Using the module:var mymodule = ...
Built-in Modules• File System require(fs);• HTTP Client and Server require(http);• TCP Sockets require(net);• Many more: h...
Getting Started• Node.js: http://nodejs.org/#download• Build instructions: http://bit.ly/egLfzu  Mac: brew update && brew ...
Reading a JSON File var fs = require(fs); var file = __dirname + /test.json; fs.readFile(file, utf8, function (err, data) ...
Simple HTTP Server var http = require(http); var s = http.createServer(function (req, res) {   var headers = {Content-Type...
Express makes HTTP easier• Nodes HTTP module is low level.• Express is a simple framework inspired by Sinatra (Ruby).• Ins...
Getting Started with Express• Generate skeleton apps:  express –help• Generate app with sessions and template engine:  exp...
Useful Modules• Express - Sinatra-like Web Framework  http://expressjs.com  npm install express• Jade - HAML-like Template...
Interesting Links• Lots of Express examples: http://bit.ly/ExpressExamples• Express Documentation: http://bit.ly/ExpressDo...
Questions? :)
Upcoming SlideShare
Loading in...5
×

Introduction to Node.js: What, why and how?

3,191

Published on

Talk given at NodeMTL #1

Published in: Technology
0 Comments
9 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,191
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
76
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide

Transcript of "Introduction to Node.js: What, why and how?"

  1. 1. Introduction to Node.js: What, why and how? Christian Joudrey - @cjoudrey
  2. 2. “Nodes goal is to provide an easy way to buildscalable network programs.”
  3. 3. What is node?• Command line tool to run JavaScript: $ node my_app.js• Built on top of Googles V8 JavaScript engine• Provides a JavaScript API for network and file system• Event-based, non-blocking I/O APIs
  4. 4. my_app.js: setTimeout(function () { console.log(world); }, 2000); console.log(hello);Running it: $ node my_app.js hello world
  5. 5. The cost of I/O?• Accessing RAM ~ 250 CPU cycles• Disk operations ~ 41 000 000 CPU cycles o Reading a file o Writing to a file• Network I/O ~ 240 000 000 CPU cycles o Database query responses o Http responses o Memcache results
  6. 6. • I/O is expensive• Most web applications are I/O bound, not CPU bound
  7. 7. Handling I/O• Apache is multithreaded (depending on conf.) o Spawns a thread per request (or process)• Each request is in its own "box", blocking I/O is "okay".result = query(SELECT * FROM ...);print result.id;
  8. 8. Thread-per-connection is expensivehttp://blog.webfaction.com/a-little-holiday-present
  9. 9. • nginx uses an event-loop, like node.
  10. 10. Enter node...• Single thread for your code• ...but, I/O runs in parallel• Handle thousands of concurrent connections with a single process• Need to be very careful with CPU-intensive code
  11. 11. I/O in node• No function should directly perform I/O.• To receive info from disk, network, or another process there must be a callback.• Callbacks are typically in the format: function (err, result) { }query(SELECT * FROM ..., function (err, result){ if (!err) { print result.id; }});
  12. 12. CommonJS Module Systemmymodule.js:module.exports.add = function (a, b) { return a + b;};Using the module:var mymodule = require(mymodule);console.log(mymodule.add(5, 2));
  13. 13. Built-in Modules• File System require(fs);• HTTP Client and Server require(http);• TCP Sockets require(net);• Many more: http://nodejs.org/docs/v0.4.8/api/
  14. 14. Getting Started• Node.js: http://nodejs.org/#download• Build instructions: http://bit.ly/egLfzu Mac: brew update && brew install node• npm, a package manager for Node: curl http://npmjs.org/install.sh | sh• Many modules at: http://search.npmjs.org/• Windows support isnt great (for the moment)
  15. 15. Reading a JSON File var fs = require(fs); var file = __dirname + /test.json; fs.readFile(file, utf8, function (err, data) { if (err) { console.log(Error: + err); return; } data = JSON.parse(data); console.dir(data); });https://gist.github.com/988107
  16. 16. Simple HTTP Server var http = require(http); var s = http.createServer(function (req, res) { var headers = {Content-Type: text/html}; req.writeHead(200, headers); res.end(<h1>hello world</h1>); }); s.listen(8080, 127.0.0.1);https://gist.github.com/988108
  17. 17. Express makes HTTP easier• Nodes HTTP module is low level.• Express is a simple framework inspired by Sinatra (Ruby).• Installing Express: npm install express && npm install jade• Robust verb-based routing o app.get(/about, function(req, res) { }); o app.post(/login, function(req, res) { });• Templating (with Jade or EJS)• Sessions
  18. 18. Getting Started with Express• Generate skeleton apps: express –help• Generate app with sessions and template engine: express --sessions --template jade hello/• Generated structure: $ ls hello/ app.js logs pids public test views $ ls hello/views/ index.jade layout.jade
  19. 19. Useful Modules• Express - Sinatra-like Web Framework http://expressjs.com npm install express• Jade - HAML-like Template Engine http://jade-lang.com npm install jade• Socket.IO - Cross-browser WebSocket library http://socket.io/ npm install socket.io
  20. 20. Interesting Links• Lots of Express examples: http://bit.ly/ExpressExamples• Express Documentation: http://bit.ly/ExpressDocs• NodeMTL.com Source Code: http://bit.ly/iZCjod• Database wrappers and ORMs: o Mongoose (MongoDB, ORM-like): https://github.com/LearnBoost/mongoose o Couch-ar (CouchDB, Active Record implementation): https://github.com/scottburch/couch-ar
  21. 21. Questions? :)
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×