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

Introduction to Node and its Core Modules

aryoung
February 06, 2013

Introduction to Node and its Core Modules

From http://bathcamp.org/, February 6th 2013

aryoung

February 06, 2013
Tweet

More Decks by aryoung

Other Decks in Programming

Transcript

  1. A

  2. Streams var zlib = require('zlib').createGzip(); var fs = require('fs'); var

    inp = fs.createReadStream('input.txt'); var out = fs.createWriteStream('output.txt.gz'); inp.pipe(gzip).pipe(out); Clean,
  3. Events var util = require('util'); var events = require('events'); function

    Player() { events.EventEmitter.call(this); } util.inherits(Player, events.EventEmitter); var player = new Player(); player.on('play', function(track) { console.log('[NP]:', track.title); }); player.emit('play', { title: 'Spit On A Stranger', artist: 'Pavement' }); Thursday, 7 February 13
  4. Servers var net = require('net'); var server = net.createServer(function(c) {

    console.log('server connected'); c.on('end', function() { console.log('server disconnected'); }); c.write('hello\r\n'); c.pipe(c); }); server.listen(8124, function() { //'listening' listener console.log('server bound'); }); Thursday, 7 February 13
  5. HTTP Server var http = require('http'); var fs = require('fs');

    http.createServer(function(req, res) { fs.readFile(__dirname + 'index.html', function(err, data) { if (err) { res.statusCode = 500; res.end(String(err)); } else { res.end(data); } }); }).listen(8000); Thursday, 7 February 13
  6. HTTP Server var http = require('http'); var fs = require('fs');

    http.createServer(function(req, res) { fs.readFile(__dirname + 'index.html', function(err, data) { if (err) { res.statusCode = 500; res.end(String(err)); } else { res.end(data); } }); }).listen(8000); Thursday, 7 February 13
  7. HTTP Server var http = require('http'); var fs = require('fs');

    http.createServer(function(req, res) { fs.readFile(__dirname + 'index.html', function(err, data) { if (err) { res.statusCode = 500; res.end(String(err)); } else { res.end(data); } }); }).listen(8000); Thursday, 7 February 13
  8. HTTP Server var http = require('http'); var fs = require('fs');

    http.createServer(function(req, res) { fs.readFile(__dirname + 'index.html', function(err, data) { if (err) { res.statusCode = 500; res.end(String(err)); } else { res.end(data); } }); }).listen(8000); Not
  9. Streams with HTTP var http = require('http'); var fs =

    require('fs'); http.createServer(function(req, res) { var stream = fs.createReadStream('index.html'); stream.pipe(res); }).listen(8000); Thursday, 7 February 13
  10. Gzip for Free var http = require('http'); var fs =

    require('fs'); var zlib = require('zlib'); http.createServer(function(req, res) { var stream = fs.createReadStream('index.html'); res.writeHead(200, { 'content-encoding': 'gzip' }); stream.pipe(zlib.createGzip()).pipe(res); }).listen(8000); Thursday, 7 February 13
  11. Gzip for Free var http = require('http'); var fs =

    require('fs'); var zlib = require('zlib'); http.createServer(function(req, res) { var stream = fs.createReadStream('index.html'); res.writeHead(200, { 'content-encoding': 'gzip' }); stream.pipe(zlib.createGzip()).pipe(res); }).listen(8000); How