This document provides an introduction to Node.js. It discusses that Node.js is an event-driven, non-blocking I/O platform for building scalable network applications using JavaScript. It was created to address issues with traditional blocking I/O by using asynchronous programming. The document outlines benefits of Node.js like using JavaScript for server-side applications, non-blocking I/O, a large module ecosystem, and an active community. It also provides examples of core modules, writing simple modules, and creating an HTTP server in Node.js.
6. Why?
Node's goal is to provide an easy way
to build scalable network programs.
- nodejs.org
7. Traditional I/O
var data = fs.readFile('large.mov');
// Wait until file.read finishes
doSomethingWith(data);
Waiting for disk operation to finish is slow!
8. Asynchronous I/O
fs.readFile('large.mov', function(data){
doSomethingWith(data);
});
// Do Something Else Right Away!
doSomethingElse();
Read from the disk, and fire a callback when done.
Meanwhile, Do Something Else!
Inspiration: Felix Geisendörfer
11. THE GOOD
• JavaScript on the Server
• Desktop / Web / CLI Applications
• Non Blocking I/O: DB, Filesystems, etc
12. THE GOOD
• JavaScript on the Server
• Write CLI / GUI Applications
• Non Blocking I/O: DB, Filesystems, etc
• NPM : Node Package Manager
13. THE GOOD
• JavaScript on the Server
• Write CLI / GUI Applications
• Non Blocking I/O: DB, Filesystems, etc
• NPM : Node Package Manager
• Active and friendly Community
20. Core Modules
• HTTP/HTTPS - Rock on!
• TCP - That’s nifty...
• DNS - Perform DNS operations
• Filesystems - read/write/watch !!
21. Core Modules
• HTTP/HTTPS - Rock on!
• TCP - That’s nifty...
• DNS - Perform DNS operations
• Filesystems - read/write/watch !!
• Crypto, SSL/TLS, Readline, etc...
22. Core Modules
• HTTP/HTTPS - Rock on!
• TCP - That’s nifty...
• DNS - Perform DNS operations
• Filesystems - read/write/watch !!
• Crypto, SSL/TLS, Readline, etc...
Over 3,400 Community Modules and counting !
http://search.npmjs.org/
23. Modules
var fs = require('fs'),
sys = require('sys'),
http = require('http'),
foo = require('./lib/foo'),
bar = require('./lib/bar');
Import system wide modules such as fs,
sys, http or local modules like /lib/foo.js
24. Roll Your Own Modules
lib/say.js
exports.say = function(something){
return something;
}
server.js
var baz = require('./lib/say');
console.log(baz.say('Foo Bar!'));
$ node server.js
Foo Bar!
25. HTTP Server
If you want to make an HTTP Server,
you must first create the universe.
- Carl Sagan
26. HTTP Server
/hello-http.js
var http = require('http')
http.createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}).listen(3000);
console.log('Hello World');
$ node hello-http.js
Server Running
29. Resources & Tutorials
List of Resources from Joyent
https://github.com/joyent/node/wiki/Resources
Node.js Beginners Guide
http://nodeguide.com/beginner.html
Read the API
http://nodejs.org/docs/latest/api/