Learn you node

Cristian Cota

Requirements

  • Node.js

  • Text editor

What is

Node.js?

​A platform which allow us to run Javascript on the computer/server.

  • Uses Javascript
  • Very fast (uses V8)
  • Huge ecosystem of source packages (npm)
  • Great for real-time apps

Why is so popular?

V8 Engine

Javascript
C++       Node.JS        V8
Machine Code

Global Object

Node

Chrome

Hello world

Functions

function greet() {
    console.log("Hello")
}
var goodBye = function() {
    console.log("Bye")
}
var callFunction = (func) {
    func()
}


callFunction(goodBye)

Modules & Require


// File 1
function countElements(arr){
    console.log('Array size is: ' + arr.lenght)
}

var myArray = ['foo', 'bar']
console.log(countElements(myArray))


// File 2
var myArray2 = ['foo', 'bar', 'foobar']
console.log(countElements(myArray2))


// File 1
module.exports = function(arr){
    console.log('Array size is: ' + arr.lenght)
}



// File 2
var count = require('./File1')

var myArray2 = ['foo', 'bar', 'foobar']
count(myArray2)

// File 1
module.exports = function(arr){
    console.log('Array size is: ' + arr.lenght)
}



// File 2
var count = require('./File1')

var myArray2 = ['foo', 'bar', 'foobar']
console.log(count(myArray2))


module.exports.sumar = sumar;
module.exports.restar = restar;
module.exports.multiplicar = multiplicar;
module.exports.dividir = dividir;

function sumar(a, b){
    return a+b;
}

function restar(a, b){
    return a-b;
}

function multiplicar(a, b){
    return a*b;
}

function dividir(a, b){
    return a/b;
}

Events




var events = require('events');
var eventEmitter = new events.EventEmitter();

var greet = function (){
    console.log('Hi you!');
}
eventEmitter.on('doorOpen', greet);

eventEmitter.emit('doorOpen');

File management




var fs = require('fs');



var fs = require('fs')
var file = fs.readFileSync('fileName', 'utf-8');

console.log(file)





var fs = require('fs')
fs.writeFileSync('newFileName', 'new file');

Asynchronous I/O






var fs = require('fs')
fs.readFile('fileName', 'utf-8', function(err, data) {
    console.log(data)
});

Client-Server

Client

Client

Request

Response


var http = require('http');

var server = http.createServer(function(req, res){
    res.writeHead(200, {'Content-Type' : 'text/plain'})
    res.end('Hi Nodeschool')
})

server.listen(3000, '127.0.0.1');
console.log('Listening 127.0.0.1:3000')

Streams and Buffers

Buffer

  • Temporary storage spot for a chunk of data that is being transferred from one place to another

  • Buffer is filled with data, then passed along

  • Transfer small chunks of data at a time

Stream

  • Flows of data you can either read from, or write to.

Stream Types

Read from a Stream

Write to a Stream

Duplex


var fs = require('fs');

var myReadStream = fs.createReadStream('./read.txt', 'utf-8');

myReadStream.on('data', function(chunk){
    console.log('new chunk recieved')
})

Read Stream


var fs = require('fs');

var myReadStream = fs.createReadStream('./read.txt', 'utf-8');
var myWriteStream = fs.createWriteStream('./write.txt');

myReadStream.on('data', function(chunk){
    console.log('new chunk recieved')
    myWriteStream.write(chunk)
})

Write Stream

Pipes




var fs = require('fs');

var myReadStream = fs.createReadStream('./read.txt', 'utf-8');
var myWriteStream = fs.createWriteStream('./write.txt');

myReadStream.pipe(myWriteStream)


var fs = require('fs');
var http = require('http');

var server = http.createServer(function(req, res){
    console.log('request was made by ' + req.url)
    res.writeHead(200, {'Content-Type' : 'text/plain'})

    var myReadStream = fs.createReadStream('./read.txt', 'utf-8');
    myReadStream.pipe(res)
})

server.listen(3000, '127.0.0.1');
console.log('Listening 127.0.0.1:3000')

Let's practice

Thank you!

ccota@nearsoft.com

github.com/cristiancota

Learnyounode

By Cristian Cota

Learnyounode

  • 260