title: node.js intro author: name: Andi Neck twitter: andineck url: http://intesso.com controls: true progress: true style: style.css output: index.html
--
feedback and pull requests welcome on GitHub
navigate with left, right keys
--
"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/
--
--
--
- build chain gulp, grunt: compile, lint, bundle, etc...
- website / webapp express, hapijs
- backend for mobile apps
- RESTful services restify
- microservices seneca
- desktop applications electron, e.g. atom, brave, visual studio code
- commandline tools npm package.json bin, commander, minimist
- messaging (TCP, UDP, http, https, ...) node.js api
- IoT Node-RED, cyclon.js
- native c, c++ modules addons
- basically any I/O, Network or Web Stuff that needs asynchronous/parallel processing
--
basically anything:
windows, linux, mac, arm, docker ...
download: https://nodejs.org/en/download/
--
- 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 ...
--
- 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
--
- 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
--
Ryan Dahls thesis
io needs to be done differently
--
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.
--
source: https://strongloop.com/strongblog/node-js-performance-event-loop-monitoring/
--
- your code is single threaded, basically everything else runs in parallel
- event-loop event-loop explanation, ryan dahl's original presentation, is based on
libuv
- EventEmitter to interact with the event-loop
- EventEmitter itself is synchronous
--
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
--
pick whatever you like!
--
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
--
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');
--
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');
--
- node.js api
- web apis devdocs
- testing node.js server + npm online tonicdev
- testing node.js browserified bundles requirebin
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