Node.js Introduction

Wenbin Ma

Outline

  • History and Background
  • Installation
  • Features
    • Async non-blocked I/O
    • Event Driven
    • CommonJS
  • Libraries and Framework

What's Node.js

Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

Background

  • Chrome's Javascript runtime is an open souce fastest javascript engine, called V8
  • Node.js run on V8
  • Create by Ray Daul in 2009

Background-Achitecture

  • libev(event loop)
  • libeio(non-block thread pool)
  • V8(Javascript engine)

Installation

Features

  • Non-blocking I/O
  • Event Driven
  • CommonJS

Blocked I/O

  • All I/Os are expensive
  • Program is blocked by I/O operation
  • Thread is used to make program paralle
  • Thread context swtiching is expensive
var data = file.read('file.data');
//waiting for read to complete...
doSomething(data)

Blocked I/O

Non-blocking I/O

  • Program returned immediately, continue execute
  • Program has an event loop
  • Program informed when data ready event emitted, through callback
file.read('file.data', function(data){
	doSomethingWithData(data);
}
doOtherThingWhileReadingData();

Non-blocking I/O

Node.js Event

  • Key feature of Node.js
  • Support async Event like Ajax
  • Derived from events.EventEmitter

Node.js Event Example

var req = http.request(options, function (res) {
    console.log('STATUS: ' + res.statusCode);
    console.log('HEADERS: ' + JSON.stringify(res.headers));
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        console.log('BODY: ' + chunk);
    });
});

Node.js Event Demo

  • Demo1: emit and handle events
  • Demo2: custom events.EventEmitter

Node.js and CommonJs

  • CommonJs specified JS standard library
  • Node.js implemented CommonJs specification

Module And Package

  • Each js file is a module
  • Variables outside the file cannot visible except their are global or exported
  • Use module.exports to write modules
  • Use require('fs') to import fs module

Module And Package 2

  • Package is a group of a modules
  • Package metadata is defined in package.json
  • package.json in the root of a package dir

NPM - manage packages

  • find a package: search in npm.org
  • install package: npm install package_name
  • npm installed package in current dir: ./node_modules/
  • npm install -g, install package to global node_modules dir

Modules and Packages

Http Module-create http server

    var http = require('http');
    var server = http.createServer(function(request, response) {
        response.write('Hello, world!');
        response.end();
    });
    var port = 3500;
    server.listen(port);
    console.log("server listen on port: " + port);
    

What Express.js can do

  • Routing
  • Middleware
    • Logger
    • Body Parser
    • Method Override
    • Cookie Paser
    • Session
  • Templating Engine
  • Static Content

Towser.js

  • Full stack web application framework
  • based on Coffeescript
  • more like Rail's framework

async.js

  • Utils module to help avoid callback nessted
  • Demo

Socket.io

  • Used to make realtime apps
  • Blurring the differences between the different transport mechanisms

Check this out!

Review

  • Node history and background
  • Node key features
    • non-blocking I/O
    • event driven
    • module and packages
  • introduce some node module and packages