Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Node.js Overview

Node.js Overview

Talk at devcamp.by at 2010

Antono Vasiljev

February 23, 2012
Tweet

More Decks by Antono Vasiljev

Other Decks in Programming

Transcript

  1. Задача Node.js: To provide a purely evented, non-blocking infrastructure to

    script highly concurrent programs. http://nodejs.org/
  2. $.ajax({ url: 'ajax/test.html', success: function(data) { $('.result').html(data); alert('Load was performed.');

    }, error: function() {}, complete: function() {}, beforeSend: function() {} });
  3. var http = require('http'); var google = http.createClient(80, 'www.google.com'); var

    request = google.request('GET', '/', {'host': 'www.google.com'}); request.end(); request.on('response', function (response) { console.log('STATUS: ' + response.statusCode); console.log('HEADERS: ' + JSON.stringify(response.headers)); response.setEncoding('utf8'); response.on('data', function (chunk) { console.log('BODY: ' + chunk); }); });
  4. Socket.io и Nordstream var http = require('http'), io = require('socket.io')

    server = http.createServer(function(req, res){ res.writeHeader(200, {'Content-Type': 'text/html'}); res.writeBody('<h1>Hello world</h1>'); res.finish(); }); // socket.io var socket = io.listen(server); socket.on('connection', function(client){ // new client is here! client.on('message', function(){ … }) client.on('disconnect', function(){ … }) });
  5. var socket = new io.Socket(); socket.on('connect', function(){ socket.send('hi!'); }) socket.on('message',

    function(data){ alert(data); }) socket.on('disconnect', function(){})
  6. Nordstream var connections = 0; var nodestream = io.listen(app).nodestream() .on('connect',

    function(){ connections++; this.emit('connections', connections); }) .on('disconnect', function(){ connections--; this.emit('connections', connections); }); :realtime(repaint: 'connections', local: 'connections') .connections - if (connections > 1) p #{connections} people are editing right now - else p You're all alone, loser