/nodejs-introduction

Introduction into node.js

Primary LanguageHTMLMIT LicenseMIT

title: node.js intro author: name: Andi Neck twitter: andineck url: http://intesso.com controls: true progress: true style: style.css output: index.html

--

node.js intro

http://intesso.github.io/nodejs-introduction

feedback and pull requests welcome on GitHub github

navigate with left, right keys

--

what is node.js

"Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world."

Source: https://nodejs.org/en/

--

who uses node.js

node rules the world and many more ...

--

trend ( jan-2014 - nov-2016 )

node is trendy

--

use cases

--

runs on

basically anything:

windows, linux, mac, arm, docker ...

download: https://nodejs.org/en/download/

--

what makes node.js stand out?

  • same language on the server and the browser
  • excellent module system npm
  • more than
    • 230'000 npm modules (Jan 2016)
    • 350'000 npm modules (Nov 2016)
  • quite good open source communities
  • sharing spirit
  • part of the linux foundation
  • and there is more ...

--

what is (was) io.js?

  • a fork of node.js
  • because developers were not happy with the development of the stewardship of joyent
  • that was merged in September 2015 into node.js 4.0
  • it lead to a well governed OSS projects
  • that includes everyone: developers, organizations, ...
  • under the umbrella of the linux foundation

--

node.js releases

  • LTS: long term support (even numbers)
    • current release (Nov 2016): V6 "Boron"
    • actively maintained for 18 month, maintenance mode afterwards
    • no more than two LTS versions at the same time
  • Stable: shorter lifespan, more updates (odd numbers)
    • current release (Nov 2016): V7
    • when Stable release becomes the next LTS, no new features or breaking changes are added

--

how was node.js started?

Ryan Dahls thesis

io needs to be done differently

original node.js presentation by Ryan Dahl

--

the cost of i/o

  L1-cache           3 cycles

  L2-cache          14 cycles

  RAM              250 cycles

  Disk      41'000'000 cycles

  Network  240'000'000 cycles

don't block on I/O access

but how?

Threads in combination with Object oriented programming can be really painful and error prone.

--

event-loop

single-threaded-code source: https://strongloop.com/strongblog/node-js-performance-event-loop-monitoring/

--

event-loop

--

more node.js building blocks

what's underneath

  • node.js is built on top of google chrome V8
  • Microsoft submitted a pull request for support with it's ChakraCore in January 2016

--

editors

pick whatever you like!

--

debugging

debugging node.js within IDE or with node-inspector or with node >= 6

# install once
npm install -g node-inspector
# use
node-debug myapp.js
# chrome inspector included with node >= 6.0
node --inspect myapp.js

restarting node.js server on code changes use nodemon or forever

# install once
npm install -g nodemon
# use
nodemon myapp.js

--

coding

file server

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

var server = http.createServer();

server.on('request', function (req, res) {
  if (req.url == '/favicon.ico') return res.end();
  var stream = fs.createReadStream(process.cwd() + req.url);
  stream.pipe(res);
});

server.listen(8000);
console.log('fileserver running at: http://localhost:8000');

--

coding

using EventEmitter

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

var server = http.createServer();
server.on('request', function (req, res) {
  if (req.url == '/favicon.ico') return res.end();
  var stream = fs.createReadStream(process.cwd() + req.url, 'utf-8');
  stream.on('data', function(data){
    res.write('\n\ngot data: ' + data);
  });
  stream.on('error', function(err){
    res.write('\n\nshit happens' + err);
  });
  stream.on('end', function(){
    res.end('\n\ndone, no more data');
  });
});

server.listen(8000);
console.log('fileserver running at: http://localhost:8000');

--

learning, learning, learning...

get some great interacitve node.js lessons:

npm install learnyounode -g
npm install workshopper -g
npm install adventure -g
npm install functional-javascript-workshop -g

--

built with node.js at intesso

  • Traffic Control System
    • Realtime UI with WebSockets (socket.io)
    • TCP/XML Interfaces
  • Intelligent Charging Station
    • Communication with OSGI bundle via 0mq
    • binding with python code with zerorpc
    • SOAP WebService Interfaces
    • Proprietary Binary Interface with Charging Station
    • Admin Web UI
  • On demand Remote Access
  • Modular Content Management System: GlintCMS, based on GlintApp
  • and many Open Source npm Modules: andineck

--

personal experiences with node.js @andineck

  • started with v0.6
  • v0.8, v0.12, v4.3 in production
  • so far, I hardly ever experienced unexpected behaviour from node.js
  • when something was not working the way I thought it should be, it was most of the time, because I didn't understand JavaScript well enough

see also: http://intesso.github.io/javascript-obstacles