NodeJS:
JavaScript On The Other Side
@imcoddy
2011-07-10
@imcoddy
2011-07-10
JavaScript runs on the server side?
You gotta be kidding me!
To provide a purely evented, non-blocking infrastructure to script highly concurrent programs.

Node.JS is not a JS framework, but a JS interpreter
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/')
6 LoC to create a http server.
L1: 3 cycles
L2: 14 cycles
RAM: 250 cycles
Disk: 41,000,000 cycles
Network: 240,000,000 cycles
The blocking way:
var result = db.query("select..");
print('I can not print until the result is returned.')
print('And the I/O is damn SLOW.');
do_something_with_result(result);
The non-blocking way:
db.query("select..", function (result) {
print('Handle the result now.');
do_something_with_result(result);
});
print('No more waiting.');
print('Do the following right away');
do_something_else();
Everything runs in parallel, EXCEPT YOUR CODE.

NodeJS brings the power of JavaScript to the server side.
Over 4700 watchers on Github
More than 1600 modules posted
60+ contributors
3 books in progress
1 command to install: npm install module-you-want
0.5 unstable version just released
Gets better everyday
Hard core CPU calculating
Large content transfer
1G memory limit due to V8 engine
var data = {}
db.getPosts(10, function(err,posts){
data.posts = posts;
});
db.getComments(10, function(err, comments){
data.comments = comments;
});
db.getTags(function(err, tags){
data.tags = tags;
})
// view.render(data)
// How do you know when everything is ready?
function Combo(callback){
this.callback = callback;
this.items = 0;
}
Combo.prototype = {
add: function(){
this.items++;
},
finishOne: function(){
this.items--;
this.check();
},
check: function(){
if(this.items === 0){
this.callback.apply(this);
}
}
};
var data = {};
var combo = new Combo(function(){
view.render(data);
});
combo.add();
db.getPosts(10, function(err,posts){
data.posts = posts;
combo.finishOne();});
combo.add();
db.getComments(10, function(err, comments){
data.comments = comments;
combo.finishOne();});
combo.add()
db.getTags(function(err, tags){
data.tags = tags;
combo.finishOne();})
self.handleQuery = function(client, message) {
var query = message.query;
Blip.query(query).all(function(results) {
client.send(
{buffer:
results.map(
function(msg) {
return {message: {
sender: msg.sender,
content: msg.content,
}
}
}
)
});
});
};
@handleQuery = (client, message) ->
Blip.query(message.query).all (results) ->
client.send buffer:
results.map (msg) ->
message:
sender: msg.sender
content: msg.content
collection = 'test'
query = {}
app.count collection,query,(count)->
doc =
name:1
app.save collection,doc,()->
console.log 'save in '+collection
app.count collection,query,(result)->
assert.eql count+1,result
collection = 'product'
// save action was supposed to execute in the 'test' collection
// but it will actually be saved in 'product' collection!
Windows porting with MS offical support
New networking layer
Long traceback
...
Linux + Node.js + MongoDB:
The new LAMP is on the way