a explosão do node.js: javascript é o novo preto

Post on 10-May-2015

2.845 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

A explosão do Node.jsJavaScript é o novo preto

Nando Vieira

Sobre mim.@fnandohttp://nandovieira.com.br

SIMPLESIDEIAS.COM.BR

codeplane.com.br

howtocode.com.br

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.

var http = require("http");

http.createServer(function (request, response) { response.end("Hello TDC 2011!\n");}).listen(2345);

Servidor HTTP - Node.js

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");

Chat TCP

Presentta.Um sistema de treinamento online.

Um monte de coisas.Node.js + Ruby + Rails + Flash + Linux + Erlang + WebSockets.

Navegação de slides

MicrofonesQualidade do

Á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 eescopo 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();

Escopo de variáveis

this.É o dono da função executada.

function someFunction(){ return this;};

someFunction() === this;

Escopo de variáveis

function someFunction(){ return this;};

var items = [];someFunction.call(items) === items;

Escopo de variáveis

Função anônima.Função que foi definida sem um nome.

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);

Funções anônimas

Closures.Funções que guardam o escopo de variáveis quando foram definidas.

var handlers = [];

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

console.log(handlers.length); // 5handlers[0](); // 5handlers[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](); // 0handlers[1](); // 1

Closures

var handlers = [];

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

handlers[0](); // 0handlers[1](); // 1

Closures

Entenda JavaScript.Node.js é fácil. JavaScript, nem tanto.

Dúvidas?

top related