Node.js –



Node.js –

0 0


nodejs-presentation


On Github camelaissani / nodejs-presentation

Node.js

Created by Camel Aissani / @camelaissani Inspired by Jeff Kunkle

What is 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.

source nodejs.org

Problematic

I/O Latency

Most of the the time the process waits for I/O (memory, disk, network)

Conventional solution

Multi-threading

  • Context switching overhead
  • Memory consumption
  • Manage concurrency
  • Dead lock

Multi processes

  • High memory usage
  • Process scheduling overhead
and always waiting I/O...

Node.JS answer

Scale with Event Loop

Delegate the I/O part to a Thread Pool or when it is possible to a native asynchronous I/O API

Event Loop theory

Delegate I/O part and manage callbacks

Platform

NPM

npm is the default package manager for Node.js.

It manages dependencies for an application.

It also allows users to install Node.js applications that are available on the npm registry.

source wikipedia

Benefits

Multi-Platform (Linux, Mac, Windows)

One development language JS on client and server side

Open source technology

Many NPM packages availables on npmjs.org

Short learning curve (Event loop theory, JavaScript, Evented programming)

Few metrics

Computation

source

Connection

source

Code

Read file and display it

var fs = require('fs');
fs.readFile('./sample.txt', 'utf8', function (err,data) {
  if (err) {
    return console.log(err);
  }
  console.log(data);
});
					

Simple http server

var http = require('http');

http.createServer(function(req, res) {
	res.writeHead(200, {'Content-Type':'text/plain'});
	res.end('Hello World');
}).listen(8000, '127.0.0.1');

console.log('Server running at http://localhost:8000/');
					

Live coding

Thank you!