SlideShare a Scribd company logo
JavaScript as a Platform
Node.js, NPM and Grunt
About
{
"firstName" : "Bertrand",
"lastName" : "Chevrier",
"online" : {
"twitter" : "@kramp",
"github" : "github.com/krampstudio",
"blog" : "krampstudio.com"
},
"pro" : {
"employer" : "CGI",
"position" : "Technical expert"
},
"loves" : ["JavaScript", "Linux", "Java", "PHP"]
}
JS, OSS & me
Follow the dealer
krampstudio.com github.com/krampstudio
Jaap : node, npm & grunt
Jaap : node, npm & grunt
Why Node.js
1. Buzzword ?
2. JS.everywhere(2013);
3. JIFSNIF
4. Huge community
Platform Repo Total
packages
Platform
years old
Avg/year since
platform
Repo
years
old
Avg/year
since repo
Python PyPI 31 066 22 1 412 10 3 106
Java Maven
central
58 272 18 3 237 9 6 474
Ruby Gems.org 56 869 18 3 159 5 11 373
Node Npm 30471 4 7617 3 10 157
stats polled the 21st of May 2013
JavaScript is fun so node is funnier
WTF is node.js ?
The illegitimate child of Google and Netscape !
Jaap : node, npm & grunt
Jaap : node, npm & grunt
Jaap : node, npm & grunt
Jaap : node, npm & grunt
2008, Ryan Dahl
How to build scalable real-time web app with simplicity ?
Architecture
node.js isn't
- A programming language
- A web framework
node.js is
- A low level lib for I/O programming
node.js design
- Non blocking I/O
- Event driven model
- JavaScript
- Unix philosophy
Inspiration
- Twisted (Python)
- Tornado (Python)
- EventMachine (Ruby)
- libevent (C)
- Nginx
Non blocking I/O
for scalability
Cost of I/O
I/O cycles
L1-cache 3
L2-cache 14
RAM 250
Disk 41 000 000
Network 240 000 000
http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop
Threaded server
Non blocking server
memory usage / concurrent connections
request p. sec. / concurrent connections
- Event driven model
- Low levels I/O APIs rewritten
- Fat process
Node is designed to be non blocking from top to bottom.
JavaScript
for simplicity
Why JS?
The good parts for node
- Functionnal language : fits the event model
- Easily extensible : redesign of non blocking APIs,
DSLable
- Becomes popular : the language of the web
Unix philosophy
KISS & Battery not included
Node provides only low level APIs
Nothing superfluous.
EcmaScript 5 + CommonJs + APIs:
- Cluster, Child_Process and Process
- Crypto
- Module, Domain, Events, Timers
- Assert, Utilities
- File System, Path, OS, VM
- HTTP, Net, URL, QueryString, etc.
- Net, DNS, UDP
- Buffer, Stream, STDIO, Readline, TTY
- REPL
nodejs.org/api
JavaScript (ES5) file import ?
Modules
Require
module.exports = {
//your object
};
var fs = require('fs');
Jaap : node, npm & grunt
Diving into node
Coding with node
Discovering the patterns
Required Hello Node.js world
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
$ node hello_node_world.js
Demo : node-htop
Polling system stats
Real time
Callbacks, Errorbacks
demo:lib/poller/mem.js#32
function async(data, callback) {
if (!data) {
callback(new Error('Empty data'));
}
//do something and then
callback(null, true);
};
async({data: 'test'}, function(err, valid){
console.log("finished");
});
Callback tree
demo:lib/poller/mem.js#32
http.createServer(function processRequest(request, response){
fs.exists("/tmp/test", function(exists){
if(exists === true){
fs.stat(file, function(err, stat){
fs.appendFile(file, log, 'utf8', function(err){
db.query(function(err, data){
//etc ...
});
});
});
}
});
}
Async management
demo:lib/statpoller.js#124
var async = require('async');
var fs = require('fs');
async.filter(['file1','file2','file3'], fs.exists, function(results){
//results => [true, false, true]
});
function f1(cb){
fs.exists('dir1', function(result){
if(result === true){
cb(null, true);
} else {
cb(new Error('Oh crap!');
}
});
}
async.series([f1, f2], function(err, results){
// results => [true, true] or null
});
Futures and promises
var q = require('q'),
fs = require('fs');
var readFile = q.node(fs.readFile);
readFile('test.txt').then(function (data) {
console.log("finished");
}).then(function(){
console.log("do something else");
});
Events
demo:lib/statpoller.js#1
var events = require('events');
var Test = function(){
events.EventEmitter.call(this);
};
util.inherits(Test, events.EventEmitter);
Test.prototype.start = function(){
this.emit('started', { 'when' : new Date() });
};
var myTest = new Test();
myTest.on('started', function(data){
console.log("test started");
});
myTest.start();
Middleware
var app = {
stack : [],
use : function(middleware){
var fn = middleware;
if('function' === typeof middleware.handle){
fn = middleware.handle;
}
if('function' === typeof fn){
this.stack.push(fn);
}
},
handle : function(data){
var i = 0, layer;
for(i in this.stack){
layer = this.stack[i];
if(!layer(data)){
break;
}
}
}
};
app.use(function(data){
console.log("Hello %s", data);
return true;
});
app.use(function(data){
console.error("Goodbye %s", data);
return false;
});
app.handle('middleware');
Connect : HTTP middlewares
demo:app.js#20
var app = connect()
.use(connect.logger('dev'))
.use(connect.static('public'))
.use(function(req, res){
res.end('hello worldn');
})
.listen(3000);
Errors
try {
//something that crash
} catch(e) {
console.error(e);
};
function(cb){
if(error)
cb(new Error("Sorry..."));
} else {
cb(null, theData);
}
}
demo:lib/statpoller.js#63
myEmitter.on('error', function(e){
console.error(e.stack);
});
var aDomain = domain.create();
aDomain.on('error', function(err){
//do wafyw with the err
});
aDomain.run(function(){
//do some errorable stuffs (throws, errbacks, etc. are catched)
});
Scaling node.js
But, wait, I've only one process...
Scale Up
Use your CPUs !
Cluster
demo:server.js
var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
for (var i = 0; i > numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
//manage worker stop
});
} else {
//I'm in a fork
}
Scale out
Load balance and share the events
Jaap : node, npm & grunt
Node Package Manager
NPM do :
packages as CommonJS modules
How ?
- Simple CLI tools
- Registry
- Web site
- Included in node.js since 0.6
- Git friendly
$ npm --help
registry.npmjs.org
npmjs.org
Module Metas
package.json
{
"name": "node-htop",
"version": "0.1.0",
"description": "Web htop like",
"main": "app.js",
"scripts": {
"test": "grunt test"
},
"dependencies": {
"express": "3.2.x",
"socket.io": "0.9.x",
"underscore": "~1.4.0"
}
}
demo package.json
Usage
$ npm search bower
$ npm info bower
$ npm install -g bower
$ npm install --save socket.io
$ npm update
$ npm publish
Scopes
global -> user -> project
Transitive dependencies
A-0.1 -> B-0.1, C-0.1
B-01 -> C-0.2
A-0.1
├── node_modules
│ ├── B-0.1
│ │ └── node_modules
│ │ └── C-0.2
│ └── C-0.1
└── package.json
npm is not capable of hatred. It loves everyone, especially you.
source
Why does npm hate me?
npm faq
Node.js is fun but...
Defects
- Young project
- Libraries hell
- Error management
- Monoculture
- Noob friendly
- Devops required
Node is a cancer
Ted Dzubia
Coffescript invokes the :
JavaScript is a Toy!
beard theory
Uncle Ben, Spiderman
With great power, comes great responsibility
Grunt, the task runner
Automate your projects
- Task based
- By configuration Gruntfile.js
- NPM friendly
- Extensible by nature
Let's do some Grrrrunt
Why Grunt?
- Wide numbers of plugins
- Simple to use
- Easy to customize
- 0.5 : will use the node-taskformat
Grunt in your stack
=
Merci
Play at home
demo source slide decks

More Related Content

PDF
npm + browserify
PPTX
2015 TechSummit Web & Cloud - Gem, NPM, Bower, Nuget, Paket - Päckchen hier, ...
PDF
Hello npm
PDF
Automate All the Things with Grunt
PDF
How to make your Webpack builds 10x faster
PDF
Production optimization with React and Webpack
PDF
Front-end development automation with Grunt
PDF
Building with Virtual Development Environments
npm + browserify
2015 TechSummit Web & Cloud - Gem, NPM, Bower, Nuget, Paket - Päckchen hier, ...
Hello npm
Automate All the Things with Grunt
How to make your Webpack builds 10x faster
Production optimization with React and Webpack
Front-end development automation with Grunt
Building with Virtual Development Environments

What's hot (19)

PPTX
Node.JS and WebSockets with Faye
PDF
The New Frontend Toolchain
PDF
Understand How Node.js and Core Features Works
PDF
10 things you should know about django
PDF
Splunk user group - automating Splunk with Ansible
PPTX
Deploy Node.js application in Heroku using Eclipse
PPTX
Introduction to NodeJS
PDF
Webinar - Matteo Manchi: Dal web al nativo: Introduzione a React Native
ODP
Tool it Up! - Session #1 - Xhprof
ODP
Version Controlling
PDF
Netflix Nebula - Gradle Summit 2014
PDF
SF Gradle Meetup - Netflix OSS
PDF
Brief Intro to Phoenix - Elixir Meetup at BukaLapak
PDF
Nuxt로 사내서비스 구현하면서 얻은 경험 공유
PPTX
Grunt and Bower
PDF
Improving WordPress Performance with Xdebug and PHP Profiling
KEY
MongoDB on CloudFoundry
PDF
MVC way to introduce Sails.js - node.js framework
PDF
Zero To Cloud (OSCon 2014)
Node.JS and WebSockets with Faye
The New Frontend Toolchain
Understand How Node.js and Core Features Works
10 things you should know about django
Splunk user group - automating Splunk with Ansible
Deploy Node.js application in Heroku using Eclipse
Introduction to NodeJS
Webinar - Matteo Manchi: Dal web al nativo: Introduzione a React Native
Tool it Up! - Session #1 - Xhprof
Version Controlling
Netflix Nebula - Gradle Summit 2014
SF Gradle Meetup - Netflix OSS
Brief Intro to Phoenix - Elixir Meetup at BukaLapak
Nuxt로 사내서비스 구현하면서 얻은 경험 공유
Grunt and Bower
Improving WordPress Performance with Xdebug and PHP Profiling
MongoDB on CloudFoundry
MVC way to introduce Sails.js - node.js framework
Zero To Cloud (OSCon 2014)
Ad

Viewers also liked (18)

PDF
Understanding of node
PDF
Introduction to node.js, npm and grunt
PPTX
Difference between-angular js-nodejs
PPTX
Javascript as a Platform
PDF
Npm: beyond 'npm i'
PDF
Npm scripts
PPTX
Startup JavaScript 8 - NPM, Express.JS
PDF
Node.js & Twitter Bootstrap Crash Course
PPTX
NodeJS guide for beginners
PDF
Getting started with AWS Lambda and the Serverless Cloud
PDF
The 7 Principles of Website Optimization
PPTX
Designing Websites With a Mobile First Approach
PDF
NodeJS for Beginner
PDF
Spring Framework - Core
PDF
The 7 Principles of Conversion Centered Design
PPTX
Containerless in the Cloud with AWS Lambda
PPTX
AWS Lambda
PDF
AWS Lambda
Understanding of node
Introduction to node.js, npm and grunt
Difference between-angular js-nodejs
Javascript as a Platform
Npm: beyond 'npm i'
Npm scripts
Startup JavaScript 8 - NPM, Express.JS
Node.js & Twitter Bootstrap Crash Course
NodeJS guide for beginners
Getting started with AWS Lambda and the Serverless Cloud
The 7 Principles of Website Optimization
Designing Websites With a Mobile First Approach
NodeJS for Beginner
Spring Framework - Core
The 7 Principles of Conversion Centered Design
Containerless in the Cloud with AWS Lambda
AWS Lambda
AWS Lambda
Ad

Similar to Jaap : node, npm & grunt (20)

PPTX
introduction to node.js
PDF
Node.js - async for the rest of us.
PDF
soft-shake.ch - Hands on Node.js
PDF
Node js introduction
PDF
Original slides from Ryan Dahl's NodeJs intro talk
PPTX
Building and Scaling Node.js Applications
PPTX
NodeJS
PPTX
Event-driven IO server-side JavaScript environment based on V8 Engine
KEY
Writing robust Node.js applications
PDF
Nodejs a-practical-introduction-oredev
PPTX
How Secure Are Docker Containers?
PDF
Node.js
ODP
Introduce about Nodejs - duyetdev.com
PPTX
Intro To Node.js
PPT
JS everywhere 2011
PPT
JavaScript Event Loop
PDF
Node azure
KEY
Practical Use of MongoDB for Node.js
PPT
Node js beginner
PPT
Logstash
introduction to node.js
Node.js - async for the rest of us.
soft-shake.ch - Hands on Node.js
Node js introduction
Original slides from Ryan Dahl's NodeJs intro talk
Building and Scaling Node.js Applications
NodeJS
Event-driven IO server-side JavaScript environment based on V8 Engine
Writing robust Node.js applications
Nodejs a-practical-introduction-oredev
How Secure Are Docker Containers?
Node.js
Introduce about Nodejs - duyetdev.com
Intro To Node.js
JS everywhere 2011
JavaScript Event Loop
Node azure
Practical Use of MongoDB for Node.js
Node js beginner
Logstash

Recently uploaded (20)

PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Machine learning based COVID-19 study performance prediction
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Digital-Transformation-Roadmap-for-Companies.pptx
Big Data Technologies - Introduction.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Approach and Philosophy of On baking technology
Machine learning based COVID-19 study performance prediction
“AI and Expert System Decision Support & Business Intelligence Systems”
sap open course for s4hana steps from ECC to s4
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Unlocking AI with Model Context Protocol (MCP)
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Programs and apps: productivity, graphics, security and other tools
Advanced methodologies resolving dimensionality complications for autism neur...
20250228 LYD VKU AI Blended-Learning.pptx
Review of recent advances in non-invasive hemoglobin estimation
Network Security Unit 5.pdf for BCA BBA.
NewMind AI Weekly Chronicles - August'25 Week I
Mobile App Security Testing_ A Comprehensive Guide.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Spectral efficient network and resource selection model in 5G networks
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...

Jaap : node, npm & grunt