On Github sydcanem / nodejs-introduction
Prof. Christian Maderazo, James Santos
Node.js is an event-driven I/O framework.
It's a simple and stable I/O platform built on top of Google's V8 javascript engine. V8 is a very complex virtual machine written by google. V8 is a javascript engine inside chrome.Node.js is an event-driven I/O framework.
Good at handling lots of different I/O connections at the same time.
Node.js is an event-driven server-side javascript I/O framework.
Good at handling lots of different I/O connections at the same time.
Achieves this by making all network I/O non-blocking and file I/O asynchronous
- Javascript has no classess
- Instead, function defines objects
function Person () {} var p = new Person()
What classes can do for us?
function Person (firstname, lastname) { this.firstname = firstname; this.lastname = lastname; } // Public method 'fullName' Person.prototype.fullName = function() { return this.firstname + this.lastname; } var p = new Person('Juan', 'dela Cruz');
var ageLimit = 50; // private module.exports = Person function Person (firstname, lastname, age) { this.firstname = firstname; this.lastname = lastname; this.age = age; } Person.prototype.belowAgeLimit = function () { return this.age < ageLimit; }
var http = require('http'), fs = require('fs'), mongoose = require('mongoose');
If you've programmed in Node, this looks familiar
var express = require('express'); var fs = require('fs'); var io = require('socket.io'); var crypto = require('crypto'); var app = express.createServer(); var staticDir = express.static; io = io.listen(app); var opts = { port: 1948, baseDir : __dirname + '/../../' }; io.sockets.on('connection', function(socket) { socket.on('slidechanged', function(slideData) { if (typeof slideData.secret == 'undefined' || slideData.secret == null || slideData.secret === '') return; if (createHash(slideData.secret) === slideData.socketId) { slideData.secret = null; socket.broadcast.emit(slideData.socketId, slideData); }; }); }); app.configure(function() { [ 'css', 'js', 'plugin', 'lib' ].forEach(function(dir) { app.use('/' + dir, staticDir(opts.baseDir + dir)); }); }); app.get("/", function(req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); fs.createReadStream(opts.baseDir + '/index.html').pipe(res); }); app.get("/token", function(req,res) { var ts = new Date().getTime(); var rand = Math.floor(Math.random()*9999999); var secret = ts.toString() + rand.toString(); res.send({secret: secret, socketId: createHash(secret)}); });
You go down to a fast food joint and order a cheeseburger, they will immediately take your order and then make you wait around until the cheeseburger is ready. In the meantime they can take other orders and start cooking cheeseburgers for other people. Imagine if you had to wait at the register for your cheeseburger, blocking all other people in line from ordering while they cooked your burger! This is called blocking I/O because all I/O (cooking cheeseburgers) happens one at a time. Node, on the other hand, is non-blocking, which means it can cook many cheeseburgers at once.
PHP
echo 'Hello'; sleep(2); echo ' world.';
Node.js
setTimeout(function () { console.log(' world.') }, 2000) console.log(' Hello');
an event-loop is a mechanism which allows you to specify what happens when a particular event occurs. You can think of it as a simple list of tasks bound to be executed when a certain event occurs.
consider this jQuery code...
$.post('/resource.json', function (data) { console.log(data) });This jQuery program performs an HTTP request resource.json. When the response comes back, an anonymous function is called (the "callback") containing the argument data. If this code is blocking execution, since browsers are single threaded, if this requests took 400ms to return, any other events happening on that timeframe would wait until data comes back. Imagine the poor user experience.
Node.js simply extends this idea to I/O operations: when you start an operation like reading a file, you can pass control to back to Node and have your code run when the data has been read. For example:
// read the file /etc/passwd, and call console.log on the returned data fs.readFile('/etc/passwd', function(err, data){ console.log(data); });
Node has small group of modules (referred to as 'node core') presented to the public as API that you are intended to write programs with.
and a lot more...
http://nodejs.org/api/Download pre-compiled binaries from http://nodejs.org/download
$ git clone https://github.com/joyent/node $ cd node $ configure $ make && make install