SlideShare une entreprise Scribd logo
1  sur  93
Télécharger pour lire hors ligne
Introduction to
Node.js
Whoami
Giovanni Lela
CTO@LinkMe
cofounder@MeanMilan
@lambrojos
http://github.com/lambrojos
Logistics
● Text editor
● node.js >= 0.10.*
● npm
● nodeschool.io
Brief history of server side js
Pre-nodejs
● Netscape enterprise server (1994)
● IIS (1996)
Not very successful
Enter node.js
● Ryan Dahl creates node.js to easily give sites
push capabilities
● 2009 - Birth of node.js
● Joyent takes control
Hype, dissatisfaction, forks
In the following years node gains substantial
traction.
But in 2014 project stagnation leads to a fork - io.js
What now?
Node.js foundation
Node is currently sponsored by the node.js
foundation, with some big names being listed in the
sponsors.
The node.js foundation itself is hosted by the linux
foundation.
Technical decisions are made by the Tecnical Steering
Commitee
Why node.js matters
It can handle a lot of concurrent I/O operations.
This is because of non-blocking I/O and javascript
own nature.
Blocking I/O analogy
Suppose you own a fast-food restaurant:
● the cashier takes the order than waits for the food
to be ready before doing anything else
● if you want to serve more customers you need
more cashiers
● this is “classical” multi-threaded parallel execution
Non blocking I/O analogy
- As soon as you place your order, it’s sent off for
someone to fulfill while the cashier is still
taking your payment.
- When you are done paying, you have to step
aside because the cashier is already looking to
service the next customer.
- When your order is complete, you will be called
back
In real life
- the node server receives a request for /about.
html
- while node waits for the filesystem to get the
file, the server thread services the next
- when the file is ready a callback function call is
inserted in the server thread message queue -
can be seen as a todo list
Everything runs in parallel, except your code
source: http://localhost:9000/images/event-loop.jpg
Event loops
There are event loop implementations for many
languages in platform. But in node.js the event
loop is the platform
One last diagram
What not to do in the event loop
Everything that is CPU- intensive:
● image / video processing
● heavy number crunching
● compressing files
● actually everything involving complex
algorithms
(Hower you can write native extensions)
What do - remote APIs
● REST/JSON Api
● quite fun to use node with non relational DB
● lots of stable and mature frameworks:
○ Express
○ Hapi
○ Sails.js
○ Loopback
○ ...
Javascript everywhere
● also your frontend can be in javascript
● the barriers between the front and the back
end look thinner - Isomorphism
● Shared rendering
● Shared models
Real time web apps
That what it was born for
● Frameworks:
○ Primus
○ Socket IO
○ Meteor
● It can also be integrated inside an existing web
stack
IoT applications
● Hardware
○ Arduino
○ Rasperry PI
○ ..
● Frameworks
○ johnny five (robotics)
○ node-red (visual integration)
○ ...
Desktop applications
Electron by github is a framework for building
desktop apps. Applications built on Electron
include:
● Atom
● Slack clients
● Visual studio code
● Kitematic (docker)
Differences from the browser
● No DOM
● No browser globals such as window, document
● No browser inconsistencies - you just have the
v8
● ES5 supported - no ugly hacks and polyfills
● ES6 getting supported
Something you don’t have in the browser
● control over the process
● streams
● dependency management
● lots of callbacks
Process
It is a global variable it allow us to interct with
the OS
● process.exit(code)
● process.env.[env variable name]
● process.argv - read command line
parameters
$ node argv.js one two three four
myArgs: [ 'one', 'two', 'three',
'four' ]
Node streams
● They emit events which represent I/O
operations
● They can be readable, writable or both (duplex)
Node streams: example
var readable = getReadableStreamSomehow();
readable.on('data', function(chunk) {
console.log('got %d bytes of data', chunk.
length);
});
readable.on('end', function() {
console.log('there will be no more
data.');
});
Combining streams w/ pipe
var fs = require('fs');
var readableStream = fs.
createReadStream('file1.txt');
var writableStream = fs.
createWriteStream('file2.txt');
readableStream.pipe
(writableStream);
Modules
Modules are managed with common.js
exports.getGreetings = function() {
return 'Hello World';
};
//example.js
var hello = require('./hello.js');
var greetings = hello.getGreetings();
Module management: NPM
Common js modules are installed with npm
● npm install
● npm uninstall
● npm updatea
Modules can be installed globally (applications),
or locally to your applications (dependencies).
Dependency management
Your application dependencies and their version
are listed in the package.json files.
It can store other informations such as version,
author, scripts to be executed during the
application lifecycle.
Node-style callbacks
a.k.a. Error-first callbacks a.k.a.
Nodebacks
function callback (err,
data) { /* ... */ }
Beware of callback hell!
Node.js as a Web server app platform
Node’s asynchronous naturemakes it an ideal
platform for web server side development:
● High I/O throughput
● Efficient and easy management of concurrent
requests
● Shares language and some tooling with the client
side
● LOTS of open source libraries and modules
Frameworks?
Node’s own http module is a bit low level.
We want to be able to avoid boilerplate while
retaining flexibility
Many, many choices
Hapi
● stable
● well documented
● well maintened
● modular
● is a good mix between flexibility and structure
● good plugin ecosystem
Who uses Hapi?
How did it start?
Hapi short for HTTP API, or so they say
Developed in Walmart Labs by a guy Named Eran
Hammer
Eran Hammer also authored OAuth
What does it do?
● Routes
● Caching
● Sessions
● Logging
● Authentication
● Plugins
Philosophy
● Configuration over code
● Three Rs:
○ Reusability - plugins, handlers
○ Reduce errors - strong validation, 100% test coverage
○ Reflection - the applications knows it’s own structure .
this makes generating documentation easier
Show me the code - routes
var Hapi = require('hapi');
var server = new Hapi.Server('localhost', 8000);
server.route({
method: 'GET',
path: '/hello/{name}',
handler: function (request, reply) {
reply('hello world');
}
});
server.start();
Config objects
The route object alse accepts a config object
which controls every aspect of the request’s
lifecycle such as validation, authentication,
caching
Its properties may depend on the plugins installed
Config example
{
config: {
handler: handlers.mapUsername,
description: 'Get todo',
notes: 'Returns a todo item by the id passed in the path',
tags: ['api'],
validate: {
params: {
username: Joi.number()
.required()
.description('the id for the todo item'),
}
}
[...]
Adding functionality with plugins
Almost every feature in hapi is isolated in a plugin.
Even core features are registred as a plugin:
● static content (inert)
● validation (joi)
● errors (Boom)
● monitoring (Good)
● … MANY more
Using plugins
server.register([
Inert,
Vision,
{
register: HapiSwagger,
options: swaggerOptions
}],
function (err) {
server.start(function(){
});
});
Writing plugins
exports.register = function (server, options, next) {
server.route({
method: 'GET',
path: '/test',
handler: function (request, reply) {
reply('test passed');
}
});
next();
};
So everything must be a Hapi plugin?
No, of course, you can use any module as long as
its framework agnostic.
Most used plugins - Inert
● Download files with reply.file(path)
● Can expose files or whole directories
server.route({
method: 'GET',
path: '/{param*}', //multi segment parameter
handler: {
directory: {
path: 'public',
listing: true
}
}
});
Template rendering - Vision
var handler = function (request, reply) {
reply.view('basic/index,html', {
title: getTitle()
...
});
};
//basic/index.html
<html>
<head>
<title>{{title}}</title>
..
Template helpers
Providing template helpers is easy
// this is the server.view config obj
{
views: {
helpersPath: 'helpers’ // path to the helpers dir
}
};
// helpers/helper.js - only one function per file!
module.exports = function(context){
// context contains the same data available to the template
return whatever(context.myvar);
}
Template helper view
<html>
<head><title>Help</title></head>
<body>
{{myhelper}} <!-- helper filename -->
</body>
</html>
Validate with Joi
● Joi is a generic validation framework
● It works by createing schemas and validating
objects against it
● It just validates the object format it does not
peform things such as database validation
● It is so powerful that documentation can be
generated from Joi schemas!
Joiful example
var schema = {
username:
Joi.string().alphanum().min(3).max(30).with('birthyear').
required(),
birthyear: Joi.number().integer().min(1900).max(2013)
};
var err = Joi.validate(data, schema, config);
console.log(err ? err : 'Valid!');
Hapi with Joi
server.route({
path: '/user/{id}',
method: 'GET',
handler: (request, reply) => {
reply(getUser(id))
},
config: {
validate: {
params: {
id: joi.number().integer()
}
}
}
Conditional validation
Joi.object({
propertyRequired: Joi.boolean().required(),
property: Joi.string().when('isGuest', {
is: true,
then: Joi.required()
}),
property2: Joi.string().alphanum()
})
.options({ allowUnknown: true })
.without('property', 'property2')
Hapi, Joi and Swagger
Joi validations can be used to generate a swagger
documentation page
This is an excellent example of reflection in action
Errors - Boom
Boom makes easy to generate HTTP errors
Boom.unauthorized([message], [scheme], [attributes])
Boom.forbidden([message], [data])
Boom.notFound([message], [data])
Boom.methodNotAllowed([message], [data])
Boom.notAcceptable([message], [data])
Boom.proxyAuthRequired([message], [data])
Boom.clientTimeout([message], [data])
Boom.conflict([message], [data])
Cookies aka server.state
server.state('session', { //cookie name
path: '/',
encoding: 'base64json',
ttl: 10,
domain: 'localhost'
});
reply('success')
.state(
'session', //cookie name
'session' //cookie value
)
Authorization strategies
Strategies are provided by plugins, and can be
plugged in routes:
server.auth.strategy(
'simple',
'basic',
{ validateFunc: validate }
);
Auth validation function
function (request, username,
password, callback) {
var isValid = username === user.
name && password === user.password;
return callback(null, isValid, {
name: user.name });
};
Time to code!
sudo npm install -g makemehapi
npm install hapi
Why debugging is important
● Javascript is an extremely flexibile and expressive
language
● Maybe a little bit too much
● We need strong tooling support
The console object
● This is our first line of defense
● Was born inside browser developer tools
● Node provides us with a preconfigured console,
exposed as global
stdin stderr
Other console methods
● dir: explore objects
● trace: print current stack trace
● time/timend: basic performance profiling
Static analysis
● A lot of errors can be discovered just by
analyzed the source code text.
● The most two common analysis are
typechecking and linting
Linting
● suspiciouse code is flagged by the linter to be
reviewed by the programmer
● you can configure what should be suspicious
● can also be used to enforce coding styles
(useful in teams)
ESLint
● Actually parses your code, instead of just
analyzing text
● Pluggable
● Integrated with most code editors
ESLint example config
{
"env" : {
"node" : true //this determines which globals to expect
},
"rules" : {
"quotes": [2, "double"],
"no-use-before-define": 2
}
}
Logging
● An emitted stream of distinct messages from a
running application
● Ordered in time
● Many applications, from debugging to
performance analysis to security
Structured logs
07/Mar/2004:16:06:51 -"GET request from
/twiki/bin/rdiff/TWiki/NewUserTemplate?rev1=1.3&rev2=1.2 HTTP/1.
1" 200 4523
vs
{“date”: “2004-04-04”, “method”: “GET”, “uri”:”
/twiki/bin/rdiff/TWiki/NewUserTemplate”: header: {
/../
}}
Structured loggers
Structured loggers log objects, not just strings.
We don’t have to restructure strings while
analyzing data
Much easier for parsing
Stack traces
It’s the record of the function calls until a certain point (usually an error).
console.log(e.stack);
Error: Something unexpected has occurred.
at main (c:UsersMeDocumentsMyAppapp.js:9:15)
at Object. (c:UsersMeDocumentsMyAppapp.js:17:1)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
The problem with async stacktraces
setTimeout(function first() {
setTimeout(function second() {
setTimeout(function third() {
a.b.c;
}, 1);
}, 1)
}, 1)
long stack traces
ReferenceError: a is not defined
at third [as _onTimeout] (/home/zio/stuff/nodeschool/bugclinic/provastack.
js:4:13)
at Timer.listOnTimeout (timers.js:92:15)
This is because we can see only the stack of the message currently processed
by the event loop
With advanced black magic, we are eventually able to get the full stack
Automated Testing
● Automated testing is incredibly cost effective
○ You get a much better understing of your code
○ You get much safer code
○ You are protected from regression
○ Tests are the most formal kind of documentation
○ You save of a LOT of time by letting the machine create
the test environment and executing the test for you
○ .. this could go on for hours
Runners and assertions
A test environment is usually composed by:
- a test runner
- an assertion library
A test is usually composed by 2+1 phase
- setup
- assertion
- teardown (optional)
Tape
We are going to use Tape
- assertion and running in the same library
- both very simple
- outputs TAP strings
Node core debugging
If for some reason you want to get node’s internal debug
message it’s easy, just prepend an env variable.
#prepare to get a lot of output
NODE_DEBUG=cluster,net,http,fs,tls,module,timers
nod node myapp
The debug module
Yo u can use a similar approach in your code with the debug module:
var debug = require('debug')('http'), http=require('http'), name='My App';
debug('booting %s', name);
http.createServer(function(req, res){
debug(req.method + ' ' + req.url);
res.end('hellon');
}).listen(3000, function(){
debug('listening');
});
Running in debug mode
Tracing vs debugging
Tracing is very specialized kind of debugging:
● It is never a functional requirement
● It is meant for the developer , not the system administrator
● It is commonly used for detecting bottlenecks, memory
leaks and other problem arising during continuous
operations.
Interactive debugging: repl
The mighty Read-Evaluate-Print-Loop
Useful to test small bits of code or playing around with
libraries.
Too long to reproduce actual application in it.. wouldn’t be
cool if you could expose a repl in certain part of your app?
Good news, you totally can!
Replify
var replify = require('replify')
, app = require('http').createServer()
replify('realtime-101', app);
The node debugger
You can start an app with the “debug” flag:
node debug myapp.js
Then the app will be in debug mode and you can:
- set breakpoints in code (debugger;)
- navigate
- execute REPLs at leisure
NPM
Npm is not only a package / dependancy manager.
● It is a HUGE repository
● It’s generic automation tool
● A place to offer / find a job
● and also a company
The site
On the site you can browse packages, search them by keyword
and get a good lot of metrics:
● number of downloads
● github statistics
● even try them out! well played NPM
this is extremely important due to impressive amount of
javascript packages out there
https://www.npmjs.com/package/lodash
Publishing on npm
● npm adduser
● npm publish to publish the package.
● Go to http://npmjs.com/package/<package>.
You can also create private modules.
Versioning
The whole npm ecosystem uses semantic versioning (semver
for friends). A semantic version number is composed by three
integers:
MAJOR.MINOR.PATCH
Semantic versioning
MAJOR is incremented when incompatible API
changes are made
MINOR is incremented when backwards
compatibile feature are added
PATCH is incremented when a bug is fixed
Semver operators
● >, <, >=, <=
● || - OR
● * - gets the most recent - dangerous
● ~ update only patch - ~0.5.0 >=0.5.0 < 0.6.0
● ^ upgrade only minor - ^ 0.5.0 >= 0.5.0 < 1.0.0
package.json
It can be seen as the “manifest” of an application
Contains many metadata used for:
● uniquely identify a package
● establish authorship
● lifecycle management
○ pre - post install scripts
○ main file
○ ...
Dissecting a package.json file
http://browsenpm.org/package.json
Time to code
git clone -b exercise https://github.
com/lambrojos/nodeintro.git
or
https://github.
com/lambrojos/nodeintro/archive/exercise.zip
npm install
Nodemon is a friend
(sudo) npm install -g nodemon
nodemon server.js (you will get errors)
solutions are in the master branch
Troubleshooting
If you get compile errors try adding the --no-
optional flag to npm install

Contenu connexe

Tendances

Node Architecture and Getting Started with Express
Node Architecture and Getting Started with ExpressNode Architecture and Getting Started with Express
Node Architecture and Getting Started with Expressjguerrero999
 
Server Side Event Driven Programming
Server Side Event Driven ProgrammingServer Side Event Driven Programming
Server Side Event Driven ProgrammingKamal Hussain
 
node.js: Javascript's in your backend
node.js: Javascript's in your backendnode.js: Javascript's in your backend
node.js: Javascript's in your backendDavid Padbury
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.jsorkaplan
 
Node.js Workshop - Sela SDP 2015
Node.js Workshop  - Sela SDP 2015Node.js Workshop  - Sela SDP 2015
Node.js Workshop - Sela SDP 2015Nir Noy
 
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming  Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming Tom Croucher
 
Building servers with Node.js
Building servers with Node.jsBuilding servers with Node.js
Building servers with Node.jsConFoo
 
Asynchronous I/O in NodeJS - new standard or challenges?
Asynchronous I/O in NodeJS - new standard or challenges?Asynchronous I/O in NodeJS - new standard or challenges?
Asynchronous I/O in NodeJS - new standard or challenges?Dinh Pham
 
Node.js Explained
Node.js ExplainedNode.js Explained
Node.js ExplainedJeff Kunkle
 
Node js presentation
Node js presentationNode js presentation
Node js presentationmartincabrera
 
Introduction to Node js
Introduction to Node jsIntroduction to Node js
Introduction to Node jsAkshay Mathur
 
All aboard the NodeJS Express
All aboard the NodeJS ExpressAll aboard the NodeJS Express
All aboard the NodeJS ExpressDavid Boyer
 
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web ApplicationsNodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web ApplicationsGanesh Iyer
 
RESTful API In Node Js using Express
RESTful API In Node Js using Express RESTful API In Node Js using Express
RESTful API In Node Js using Express Jeetendra singh
 
Original slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talkOriginal slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talkAarti Parikh
 
A million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scaleA million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scaleTom Croucher
 
Non-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.jsNon-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.jsMarcus Frödin
 
Java script at backend nodejs
Java script at backend   nodejsJava script at backend   nodejs
Java script at backend nodejsAmit Thakkar
 

Tendances (20)

Node Architecture and Getting Started with Express
Node Architecture and Getting Started with ExpressNode Architecture and Getting Started with Express
Node Architecture and Getting Started with Express
 
Server Side Event Driven Programming
Server Side Event Driven ProgrammingServer Side Event Driven Programming
Server Side Event Driven Programming
 
node.js: Javascript's in your backend
node.js: Javascript's in your backendnode.js: Javascript's in your backend
node.js: Javascript's in your backend
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.js
 
Node.js Workshop - Sela SDP 2015
Node.js Workshop  - Sela SDP 2015Node.js Workshop  - Sela SDP 2015
Node.js Workshop - Sela SDP 2015
 
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming  Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
 
Building servers with Node.js
Building servers with Node.jsBuilding servers with Node.js
Building servers with Node.js
 
Asynchronous I/O in NodeJS - new standard or challenges?
Asynchronous I/O in NodeJS - new standard or challenges?Asynchronous I/O in NodeJS - new standard or challenges?
Asynchronous I/O in NodeJS - new standard or challenges?
 
Node.js Explained
Node.js ExplainedNode.js Explained
Node.js Explained
 
Node js presentation
Node js presentationNode js presentation
Node js presentation
 
Introduction to Node js
Introduction to Node jsIntroduction to Node js
Introduction to Node js
 
All aboard the NodeJS Express
All aboard the NodeJS ExpressAll aboard the NodeJS Express
All aboard the NodeJS Express
 
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web ApplicationsNodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
 
RESTful API In Node Js using Express
RESTful API In Node Js using Express RESTful API In Node Js using Express
RESTful API In Node Js using Express
 
Original slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talkOriginal slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talk
 
A million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scaleA million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scale
 
Nodejs presentation
Nodejs presentationNodejs presentation
Nodejs presentation
 
Node ppt
Node pptNode ppt
Node ppt
 
Non-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.jsNon-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.js
 
Java script at backend nodejs
Java script at backend   nodejsJava script at backend   nodejs
Java script at backend nodejs
 

Similaire à NodeJS

Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.jsSu Zin Kyaw
 
Introduction to Node (15th May 2017)
Introduction to Node (15th May 2017)Introduction to Node (15th May 2017)
Introduction to Node (15th May 2017)Lucas Jellema
 
202107 - Orion introduction - COSCUP
202107 - Orion introduction - COSCUP202107 - Orion introduction - COSCUP
202107 - Orion introduction - COSCUPRonald Hsu
 
An Overview of Node.js
An Overview of Node.jsAn Overview of Node.js
An Overview of Node.jsAyush Mishra
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.jsVikash Singh
 
Introduction to Node.js: What, why and how?
Introduction to Node.js: What, why and how?Introduction to Node.js: What, why and how?
Introduction to Node.js: What, why and how?Christian Joudrey
 
Introduction to node.js GDD
Introduction to node.js GDDIntroduction to node.js GDD
Introduction to node.js GDDSudar Muthu
 
IT Operations for Web Developers
IT Operations for Web DevelopersIT Operations for Web Developers
IT Operations for Web DevelopersMahmoud Said
 
Using the new WordPress REST API
Using the new WordPress REST APIUsing the new WordPress REST API
Using the new WordPress REST APICaldera Labs
 
Normalizing x pages web development
Normalizing x pages web development Normalizing x pages web development
Normalizing x pages web development Shean McManus
 
Cloud Native Development
Cloud Native DevelopmentCloud Native Development
Cloud Native DevelopmentManuel Garcia
 
OSDC.no 2015 introduction to node.js workshop
OSDC.no 2015 introduction to node.js workshopOSDC.no 2015 introduction to node.js workshop
OSDC.no 2015 introduction to node.js workshopleffen
 
Nodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredevNodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredevFelix Geisendörfer
 
node.js 실무 - node js in practice by Jesang Yoon
node.js 실무 - node js in practice by Jesang Yoonnode.js 실무 - node js in practice by Jesang Yoon
node.js 실무 - node js in practice by Jesang YoonJesang Yoon
 

Similaire à NodeJS (20)

node.js - Fast event based web application development
node.js - Fast event based web application developmentnode.js - Fast event based web application development
node.js - Fast event based web application development
 
Nodejs
NodejsNodejs
Nodejs
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.js
 
Node.js Course 2 of 2 - Advanced techniques
Node.js Course 2 of 2 - Advanced techniquesNode.js Course 2 of 2 - Advanced techniques
Node.js Course 2 of 2 - Advanced techniques
 
Introduction to Node (15th May 2017)
Introduction to Node (15th May 2017)Introduction to Node (15th May 2017)
Introduction to Node (15th May 2017)
 
202107 - Orion introduction - COSCUP
202107 - Orion introduction - COSCUP202107 - Orion introduction - COSCUP
202107 - Orion introduction - COSCUP
 
An Overview of Node.js
An Overview of Node.jsAn Overview of Node.js
An Overview of Node.js
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Introduction to Node.js: What, why and how?
Introduction to Node.js: What, why and how?Introduction to Node.js: What, why and how?
Introduction to Node.js: What, why and how?
 
Introduction to node.js GDD
Introduction to node.js GDDIntroduction to node.js GDD
Introduction to node.js GDD
 
IT Operations for Web Developers
IT Operations for Web DevelopersIT Operations for Web Developers
IT Operations for Web Developers
 
Using the new WordPress REST API
Using the new WordPress REST APIUsing the new WordPress REST API
Using the new WordPress REST API
 
Normalizing x pages web development
Normalizing x pages web development Normalizing x pages web development
Normalizing x pages web development
 
Cloud Native Development
Cloud Native DevelopmentCloud Native Development
Cloud Native Development
 
Sprint 17
Sprint 17Sprint 17
Sprint 17
 
Exploring Node.jS
Exploring Node.jSExploring Node.jS
Exploring Node.jS
 
OSDC.no 2015 introduction to node.js workshop
OSDC.no 2015 introduction to node.js workshopOSDC.no 2015 introduction to node.js workshop
OSDC.no 2015 introduction to node.js workshop
 
Nodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredevNodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredev
 
Knolx session
Knolx sessionKnolx session
Knolx session
 
node.js 실무 - node js in practice by Jesang Yoon
node.js 실무 - node js in practice by Jesang Yoonnode.js 실무 - node js in practice by Jesang Yoon
node.js 실무 - node js in practice by Jesang Yoon
 

Plus de LinkMe Srl

Adventures in docker compose
Adventures in docker composeAdventures in docker compose
Adventures in docker composeLinkMe Srl
 
Corso su ReactJS
Corso su ReactJSCorso su ReactJS
Corso su ReactJSLinkMe Srl
 
A React Journey
A React JourneyA React Journey
A React JourneyLinkMe Srl
 
Angular Intermediate
Angular IntermediateAngular Intermediate
Angular IntermediateLinkMe Srl
 
Angular js quickstart
Angular js quickstartAngular js quickstart
Angular js quickstartLinkMe Srl
 
M&M - MeanMilan @CodeMotionMilan
M&M - MeanMilan @CodeMotionMilanM&M - MeanMilan @CodeMotionMilan
M&M - MeanMilan @CodeMotionMilanLinkMe Srl
 
Presentazione Codemotion
Presentazione Codemotion Presentazione Codemotion
Presentazione Codemotion LinkMe Srl
 

Plus de LinkMe Srl (8)

Adventures in docker compose
Adventures in docker composeAdventures in docker compose
Adventures in docker compose
 
Corso su ReactJS
Corso su ReactJSCorso su ReactJS
Corso su ReactJS
 
A React Journey
A React JourneyA React Journey
A React Journey
 
Webdriver.io
Webdriver.io Webdriver.io
Webdriver.io
 
Angular Intermediate
Angular IntermediateAngular Intermediate
Angular Intermediate
 
Angular js quickstart
Angular js quickstartAngular js quickstart
Angular js quickstart
 
M&M - MeanMilan @CodeMotionMilan
M&M - MeanMilan @CodeMotionMilanM&M - MeanMilan @CodeMotionMilan
M&M - MeanMilan @CodeMotionMilan
 
Presentazione Codemotion
Presentazione Codemotion Presentazione Codemotion
Presentazione Codemotion
 

Dernier

MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 

Dernier (20)

MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 

NodeJS

  • 3. Logistics ● Text editor ● node.js >= 0.10.* ● npm ● nodeschool.io
  • 4. Brief history of server side js Pre-nodejs ● Netscape enterprise server (1994) ● IIS (1996) Not very successful
  • 5. Enter node.js ● Ryan Dahl creates node.js to easily give sites push capabilities ● 2009 - Birth of node.js ● Joyent takes control
  • 6. Hype, dissatisfaction, forks In the following years node gains substantial traction. But in 2014 project stagnation leads to a fork - io.js What now?
  • 7. Node.js foundation Node is currently sponsored by the node.js foundation, with some big names being listed in the sponsors. The node.js foundation itself is hosted by the linux foundation. Technical decisions are made by the Tecnical Steering Commitee
  • 8. Why node.js matters It can handle a lot of concurrent I/O operations. This is because of non-blocking I/O and javascript own nature.
  • 9. Blocking I/O analogy Suppose you own a fast-food restaurant: ● the cashier takes the order than waits for the food to be ready before doing anything else ● if you want to serve more customers you need more cashiers ● this is “classical” multi-threaded parallel execution
  • 10. Non blocking I/O analogy - As soon as you place your order, it’s sent off for someone to fulfill while the cashier is still taking your payment. - When you are done paying, you have to step aside because the cashier is already looking to service the next customer. - When your order is complete, you will be called back
  • 11. In real life - the node server receives a request for /about. html - while node waits for the filesystem to get the file, the server thread services the next - when the file is ready a callback function call is inserted in the server thread message queue - can be seen as a todo list
  • 12. Everything runs in parallel, except your code source: http://localhost:9000/images/event-loop.jpg
  • 13. Event loops There are event loop implementations for many languages in platform. But in node.js the event loop is the platform
  • 15. What not to do in the event loop Everything that is CPU- intensive: ● image / video processing ● heavy number crunching ● compressing files ● actually everything involving complex algorithms (Hower you can write native extensions)
  • 16. What do - remote APIs ● REST/JSON Api ● quite fun to use node with non relational DB ● lots of stable and mature frameworks: ○ Express ○ Hapi ○ Sails.js ○ Loopback ○ ...
  • 17. Javascript everywhere ● also your frontend can be in javascript ● the barriers between the front and the back end look thinner - Isomorphism ● Shared rendering ● Shared models
  • 18. Real time web apps That what it was born for ● Frameworks: ○ Primus ○ Socket IO ○ Meteor ● It can also be integrated inside an existing web stack
  • 19. IoT applications ● Hardware ○ Arduino ○ Rasperry PI ○ .. ● Frameworks ○ johnny five (robotics) ○ node-red (visual integration) ○ ...
  • 20. Desktop applications Electron by github is a framework for building desktop apps. Applications built on Electron include: ● Atom ● Slack clients ● Visual studio code ● Kitematic (docker)
  • 21. Differences from the browser ● No DOM ● No browser globals such as window, document ● No browser inconsistencies - you just have the v8 ● ES5 supported - no ugly hacks and polyfills ● ES6 getting supported
  • 22. Something you don’t have in the browser ● control over the process ● streams ● dependency management ● lots of callbacks
  • 23. Process It is a global variable it allow us to interct with the OS ● process.exit(code) ● process.env.[env variable name] ● process.argv - read command line parameters $ node argv.js one two three four myArgs: [ 'one', 'two', 'three', 'four' ]
  • 24. Node streams ● They emit events which represent I/O operations ● They can be readable, writable or both (duplex)
  • 25. Node streams: example var readable = getReadableStreamSomehow(); readable.on('data', function(chunk) { console.log('got %d bytes of data', chunk. length); }); readable.on('end', function() { console.log('there will be no more data.'); });
  • 26. Combining streams w/ pipe var fs = require('fs'); var readableStream = fs. createReadStream('file1.txt'); var writableStream = fs. createWriteStream('file2.txt'); readableStream.pipe (writableStream);
  • 27. Modules Modules are managed with common.js exports.getGreetings = function() { return 'Hello World'; }; //example.js var hello = require('./hello.js'); var greetings = hello.getGreetings();
  • 28. Module management: NPM Common js modules are installed with npm ● npm install ● npm uninstall ● npm updatea Modules can be installed globally (applications), or locally to your applications (dependencies).
  • 29. Dependency management Your application dependencies and their version are listed in the package.json files. It can store other informations such as version, author, scripts to be executed during the application lifecycle.
  • 30. Node-style callbacks a.k.a. Error-first callbacks a.k.a. Nodebacks function callback (err, data) { /* ... */ } Beware of callback hell!
  • 31. Node.js as a Web server app platform Node’s asynchronous naturemakes it an ideal platform for web server side development: ● High I/O throughput ● Efficient and easy management of concurrent requests ● Shares language and some tooling with the client side ● LOTS of open source libraries and modules
  • 32. Frameworks? Node’s own http module is a bit low level. We want to be able to avoid boilerplate while retaining flexibility Many, many choices
  • 33. Hapi ● stable ● well documented ● well maintened ● modular ● is a good mix between flexibility and structure ● good plugin ecosystem
  • 35. How did it start? Hapi short for HTTP API, or so they say Developed in Walmart Labs by a guy Named Eran Hammer Eran Hammer also authored OAuth
  • 36. What does it do? ● Routes ● Caching ● Sessions ● Logging ● Authentication ● Plugins
  • 37. Philosophy ● Configuration over code ● Three Rs: ○ Reusability - plugins, handlers ○ Reduce errors - strong validation, 100% test coverage ○ Reflection - the applications knows it’s own structure . this makes generating documentation easier
  • 38. Show me the code - routes var Hapi = require('hapi'); var server = new Hapi.Server('localhost', 8000); server.route({ method: 'GET', path: '/hello/{name}', handler: function (request, reply) { reply('hello world'); } }); server.start();
  • 39. Config objects The route object alse accepts a config object which controls every aspect of the request’s lifecycle such as validation, authentication, caching Its properties may depend on the plugins installed
  • 40. Config example { config: { handler: handlers.mapUsername, description: 'Get todo', notes: 'Returns a todo item by the id passed in the path', tags: ['api'], validate: { params: { username: Joi.number() .required() .description('the id for the todo item'), } } [...]
  • 41. Adding functionality with plugins Almost every feature in hapi is isolated in a plugin. Even core features are registred as a plugin: ● static content (inert) ● validation (joi) ● errors (Boom) ● monitoring (Good) ● … MANY more
  • 42. Using plugins server.register([ Inert, Vision, { register: HapiSwagger, options: swaggerOptions }], function (err) { server.start(function(){ }); });
  • 43. Writing plugins exports.register = function (server, options, next) { server.route({ method: 'GET', path: '/test', handler: function (request, reply) { reply('test passed'); } }); next(); };
  • 44. So everything must be a Hapi plugin? No, of course, you can use any module as long as its framework agnostic.
  • 45. Most used plugins - Inert ● Download files with reply.file(path) ● Can expose files or whole directories server.route({ method: 'GET', path: '/{param*}', //multi segment parameter handler: { directory: { path: 'public', listing: true } } });
  • 46. Template rendering - Vision var handler = function (request, reply) { reply.view('basic/index,html', { title: getTitle() ... }); }; //basic/index.html <html> <head> <title>{{title}}</title> ..
  • 47. Template helpers Providing template helpers is easy // this is the server.view config obj { views: { helpersPath: 'helpers’ // path to the helpers dir } }; // helpers/helper.js - only one function per file! module.exports = function(context){ // context contains the same data available to the template return whatever(context.myvar); }
  • 49. Validate with Joi ● Joi is a generic validation framework ● It works by createing schemas and validating objects against it ● It just validates the object format it does not peform things such as database validation ● It is so powerful that documentation can be generated from Joi schemas!
  • 50. Joiful example var schema = { username: Joi.string().alphanum().min(3).max(30).with('birthyear'). required(), birthyear: Joi.number().integer().min(1900).max(2013) }; var err = Joi.validate(data, schema, config); console.log(err ? err : 'Valid!');
  • 51. Hapi with Joi server.route({ path: '/user/{id}', method: 'GET', handler: (request, reply) => { reply(getUser(id)) }, config: { validate: { params: { id: joi.number().integer() } } }
  • 52. Conditional validation Joi.object({ propertyRequired: Joi.boolean().required(), property: Joi.string().when('isGuest', { is: true, then: Joi.required() }), property2: Joi.string().alphanum() }) .options({ allowUnknown: true }) .without('property', 'property2')
  • 53. Hapi, Joi and Swagger Joi validations can be used to generate a swagger documentation page This is an excellent example of reflection in action
  • 54. Errors - Boom Boom makes easy to generate HTTP errors Boom.unauthorized([message], [scheme], [attributes]) Boom.forbidden([message], [data]) Boom.notFound([message], [data]) Boom.methodNotAllowed([message], [data]) Boom.notAcceptable([message], [data]) Boom.proxyAuthRequired([message], [data]) Boom.clientTimeout([message], [data]) Boom.conflict([message], [data])
  • 55. Cookies aka server.state server.state('session', { //cookie name path: '/', encoding: 'base64json', ttl: 10, domain: 'localhost' }); reply('success') .state( 'session', //cookie name 'session' //cookie value )
  • 56. Authorization strategies Strategies are provided by plugins, and can be plugged in routes: server.auth.strategy( 'simple', 'basic', { validateFunc: validate } );
  • 57. Auth validation function function (request, username, password, callback) { var isValid = username === user. name && password === user.password; return callback(null, isValid, { name: user.name }); };
  • 58. Time to code! sudo npm install -g makemehapi npm install hapi
  • 59. Why debugging is important ● Javascript is an extremely flexibile and expressive language ● Maybe a little bit too much ● We need strong tooling support
  • 60. The console object ● This is our first line of defense ● Was born inside browser developer tools ● Node provides us with a preconfigured console, exposed as global
  • 62. Other console methods ● dir: explore objects ● trace: print current stack trace ● time/timend: basic performance profiling
  • 63. Static analysis ● A lot of errors can be discovered just by analyzed the source code text. ● The most two common analysis are typechecking and linting
  • 64. Linting ● suspiciouse code is flagged by the linter to be reviewed by the programmer ● you can configure what should be suspicious ● can also be used to enforce coding styles (useful in teams)
  • 65. ESLint ● Actually parses your code, instead of just analyzing text ● Pluggable ● Integrated with most code editors
  • 66. ESLint example config { "env" : { "node" : true //this determines which globals to expect }, "rules" : { "quotes": [2, "double"], "no-use-before-define": 2 } }
  • 67. Logging ● An emitted stream of distinct messages from a running application ● Ordered in time ● Many applications, from debugging to performance analysis to security
  • 68. Structured logs 07/Mar/2004:16:06:51 -"GET request from /twiki/bin/rdiff/TWiki/NewUserTemplate?rev1=1.3&rev2=1.2 HTTP/1. 1" 200 4523 vs {“date”: “2004-04-04”, “method”: “GET”, “uri”:” /twiki/bin/rdiff/TWiki/NewUserTemplate”: header: { /../ }}
  • 69. Structured loggers Structured loggers log objects, not just strings. We don’t have to restructure strings while analyzing data Much easier for parsing
  • 70. Stack traces It’s the record of the function calls until a certain point (usually an error). console.log(e.stack); Error: Something unexpected has occurred. at main (c:UsersMeDocumentsMyAppapp.js:9:15) at Object. (c:UsersMeDocumentsMyAppapp.js:17:1) at Module._compile (module.js:460:26) at Object.Module._extensions..js (module.js:478:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Function.Module.runMain (module.js:501:10) at startup (node.js:129:16) at node.js:814:3
  • 71. The problem with async stacktraces setTimeout(function first() { setTimeout(function second() { setTimeout(function third() { a.b.c; }, 1); }, 1) }, 1)
  • 72. long stack traces ReferenceError: a is not defined at third [as _onTimeout] (/home/zio/stuff/nodeschool/bugclinic/provastack. js:4:13) at Timer.listOnTimeout (timers.js:92:15) This is because we can see only the stack of the message currently processed by the event loop With advanced black magic, we are eventually able to get the full stack
  • 73. Automated Testing ● Automated testing is incredibly cost effective ○ You get a much better understing of your code ○ You get much safer code ○ You are protected from regression ○ Tests are the most formal kind of documentation ○ You save of a LOT of time by letting the machine create the test environment and executing the test for you ○ .. this could go on for hours
  • 74. Runners and assertions A test environment is usually composed by: - a test runner - an assertion library A test is usually composed by 2+1 phase - setup - assertion - teardown (optional)
  • 75. Tape We are going to use Tape - assertion and running in the same library - both very simple - outputs TAP strings
  • 76. Node core debugging If for some reason you want to get node’s internal debug message it’s easy, just prepend an env variable. #prepare to get a lot of output NODE_DEBUG=cluster,net,http,fs,tls,module,timers nod node myapp
  • 77. The debug module Yo u can use a similar approach in your code with the debug module: var debug = require('debug')('http'), http=require('http'), name='My App'; debug('booting %s', name); http.createServer(function(req, res){ debug(req.method + ' ' + req.url); res.end('hellon'); }).listen(3000, function(){ debug('listening'); });
  • 79. Tracing vs debugging Tracing is very specialized kind of debugging: ● It is never a functional requirement ● It is meant for the developer , not the system administrator ● It is commonly used for detecting bottlenecks, memory leaks and other problem arising during continuous operations.
  • 80. Interactive debugging: repl The mighty Read-Evaluate-Print-Loop Useful to test small bits of code or playing around with libraries. Too long to reproduce actual application in it.. wouldn’t be cool if you could expose a repl in certain part of your app? Good news, you totally can!
  • 81. Replify var replify = require('replify') , app = require('http').createServer() replify('realtime-101', app);
  • 82. The node debugger You can start an app with the “debug” flag: node debug myapp.js Then the app will be in debug mode and you can: - set breakpoints in code (debugger;) - navigate - execute REPLs at leisure
  • 83. NPM Npm is not only a package / dependancy manager. ● It is a HUGE repository ● It’s generic automation tool ● A place to offer / find a job ● and also a company
  • 84. The site On the site you can browse packages, search them by keyword and get a good lot of metrics: ● number of downloads ● github statistics ● even try them out! well played NPM this is extremely important due to impressive amount of javascript packages out there https://www.npmjs.com/package/lodash
  • 85. Publishing on npm ● npm adduser ● npm publish to publish the package. ● Go to http://npmjs.com/package/<package>. You can also create private modules.
  • 86. Versioning The whole npm ecosystem uses semantic versioning (semver for friends). A semantic version number is composed by three integers: MAJOR.MINOR.PATCH
  • 87. Semantic versioning MAJOR is incremented when incompatible API changes are made MINOR is incremented when backwards compatibile feature are added PATCH is incremented when a bug is fixed
  • 88. Semver operators ● >, <, >=, <= ● || - OR ● * - gets the most recent - dangerous ● ~ update only patch - ~0.5.0 >=0.5.0 < 0.6.0 ● ^ upgrade only minor - ^ 0.5.0 >= 0.5.0 < 1.0.0
  • 89. package.json It can be seen as the “manifest” of an application Contains many metadata used for: ● uniquely identify a package ● establish authorship ● lifecycle management ○ pre - post install scripts ○ main file ○ ...
  • 90. Dissecting a package.json file http://browsenpm.org/package.json
  • 91. Time to code git clone -b exercise https://github. com/lambrojos/nodeintro.git or https://github. com/lambrojos/nodeintro/archive/exercise.zip npm install
  • 92. Nodemon is a friend (sudo) npm install -g nodemon nodemon server.js (you will get errors) solutions are in the master branch
  • 93. Troubleshooting If you get compile errors try adding the --no- optional flag to npm install