A explosão do Node.js
      JavaScript é o novo preto
                      Nando Vieira
Sobre mim.
@fnando
http://nandovieira.com.br
SIMPLESIDEIAS.COM.BR
codeplane.com.br
ode.c om.br
how   toc
Standard library.
Timers, Process, Events, Util, Buffer,
Crypto, TLS/SSL, FileSystem, Net, DNS,
HTTP/HTTPS, URL, UDP.
Ecossistema.
O Node.js é novo, mas já tem muita coisa.
Database.
Mysql, Redis, MongoDB, CouchDB.
HTTP.
Connect e Express.
Templating.
Jade, EJS, Haml, Sass.
Realtime.
módulo net, Socket.IO, Now.js, Faye.
Deploy.
No.de, Nodester, Nodejitsu, Duostack,
Heroku, Cluster, Upstart/init.d.
Servidor HTTP - Node.js


var http = require("http");

http.createServer(function (request, response) {
  response.end("Hello TDC 2011!n");
}).listen(2345);
Chat TCP
var net = require("net")
  , emitter = new process.EventEmitter()
;

net.createServer(function(socket){
  emitter.on("message", function(sender, message){
    if (socket.writable) {
      socket.write(socket.remoteAddress + "> " + message);
    }
  });

  socket.on("data", function(data){
    emitter.emit("message", socket, data)
  });
}).listen(2345, "kernelpanic.local");
Presentta.
Um sistema de treinamento online.
Um monte de coisas.
Node.js + Ruby + Rails + Flash + Linux +
Erlang + WebSockets.
Microfones
                                            Qualidade do
Navegação de slides                            Áudio




                                                   !"#$
                      Ativa screensharing
Flash.
Streaming de áudio e vídeo.
Comunicação.
Protocolo JSON.
JSON.stringify({
  type: "new_message",
  message: "My new message",
  user: 1
});
JSON.load(payload);
JavaScript.
Closures, funções anônimas e
escopo de variáveis.
Escopo de variáveis.
Escopo de variáveis


var someVariable = "global variable!";

console.log(someVariable); // global variable

function someFunction() {
  console.log(someVariable); // global variable
}

someFunction();
Escopo de variáveis

var someVariable = "global variable!";

console.log(someVariable); // global variable

function someFunction() {
  var someVariable = "local variable";
  console.log(someVariable); // local variable
}

someFunction();
Escopo de variáveis

var someVariable = "global variable!";

console.log(someVariable); // global variable

function someFunction() {
  var someVariable = "local variable";
  console.log(someVariable); // local variable
  console.log(this.someVariable); // global variable
}

someFunction();
this.
É o dono da função executada.
Escopo de variáveis




function someFunction(){
   return this;
};

someFunction() === this;
Escopo de variáveis



function someFunction(){
   return this;
};

var items = [];
someFunction.call(items) === items;
Função anônima.
Função que foi definida sem um nome.
Funções anônimas




function someFunction() {
  // your code
}

someFunction.name; // "someFunction"
Funções anônimas




var someFunction = function() {
   // your code
};

someFunction.name; // ""
Funções anônimas




setTimeout(function(){
  // your code
});
Funções anônimas




(function(){
  // your code
})();
Funções anônimas




http.createServer(function(req, res){
  // add listeners
}).listen(2345);
Closures.
Funções que guardam o escopo de
variáveis quando foram definidas.
Closures

var handlers = [];

for (var i = 0; i < 5; i++) {
  handlers.push(function(){
    console.log(i);;
  });
}

console.log(handlers.length); // 5
handlers[0](); // 5
handlers[1](); // 5
Closures
var handlers = [];

function handler(number) {
  return function() { console.log(number); };
}

for (var i = 0; i < 5; i++) {
  handlers.push(handler(i));
}

handlers[0](); // 0
handlers[1](); // 1
Closures
var handlers = [];

for (var i = 0; i < 5; i++) {
  (function(number){
    handlers.push(function(){
      console.log(number);
    });
  })(i);
}

handlers[0](); // 0
handlers[1](); // 1
Entenda JavaScript.
Node.js é fácil. JavaScript, nem tanto.
Dúvidas?
nandovieira.com.br
simplesideias.com.br
   howtocode.com.br
            @fnando

A explosão do Node.js: JavaScript é o novo preto