you don't know node: quick intro to 6 core features

124
YOU DON'T KNOW NODE QUICK INTRO TO 6 CORE FEATURES 1 — © Capital One, 2016

Upload: great-wide-open

Post on 15-Apr-2017

141 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: You Don't Know Node: Quick Intro to 6 Core Features

YOU DON'T KNOW NODE

QUICK INTRO TO 6 CORE FEATURES

1 — © Capital One, 2016

Page 2: You Don't Know Node: Quick Intro to 6 Core Features

BETTER APPS—BETTER LIFE

2 — © Capital One, 2016

Page 3: You Don't Know Node: Quick Intro to 6 Core Features

ABOUT PRESENTER

Azat Mardan

Twitter: @azat_coEmail: [email protected]

Blog: webapplog.com3 — © Capital One, 2016

Page 4: You Don't Know Node: Quick Intro to 6 Core Features

ABOUT PRESENTER

> Work: Technology Fellow at Capital One> Experience: FDIC, NIH, DocuSign, HackReactor and Storify> Books: React Quickly, Practical Node.js, Pro Express.js,

Express.js API and 8 others> Teach: NodeProgram.com

4 — © Capital One, 2016

Page 5: You Don't Know Node: Quick Intro to 6 Core Features

5 — © Capital One, 2016

Page 6: You Don't Know Node: Quick Intro to 6 Core Features

STARTING WITH BASICS: WHY USE NODE?

6 — © Capital One, 2016

Page 7: You Don't Know Node: Quick Intro to 6 Core Features

MOST APPS WAIT FOR

INPUT/OUTPUT WHICH ARE THE

MOST 7 — © Capital One, 2016

Page 8: You Don't Know Node: Quick Intro to 6 Core Features

JAVA SLEEP

System.out.println("Step: 1");System.out.println("Step: 2");Thread.sleep(1000);System.out.println("Step: 3");

8 — © Capital One, 2016

Page 9: You Don't Know Node: Quick Intro to 6 Core Features

9 — © Capital One, 2016

Page 10: You Don't Know Node: Quick Intro to 6 Core Features

NODE "SLEEP"

console.log('Step: 1')setTimeout(function () { console.log('Step: 3')}, 1000)console.log('Step: 2')

10 — © Capital One, 2016

Page 11: You Don't Know Node: Quick Intro to 6 Core Features

PROCESS MULTIPLE TASKS

console.log('Step: 1')setTimeout(function () { console.log('Step: 3') // console.log('Step 5')}, 1000);console.log('Step: 2')// console.log('Step 4')

11 — © Capital One, 2016

Page 12: You Don't Know Node: Quick Intro to 6 Core Features

EVENT LOOP12 — © Capital One, 2016

Page 13: You Don't Know Node: Quick Intro to 6 Core Features

13 — © Capital One, 2016

Page 14: You Don't Know Node: Quick Intro to 6 Core Features

[MUTLI-THREADING] IS THE SOFTWARE EQUIVALENT OF A NUCLEAR DEVICE

BECAUSE IF IT IS USED INCORRECTLY, IT CAN BLOW UP IN YOUR FACE.

http://blog.codinghorror.com/threading-concurrency-and-the-most-powerful-psychokinetic-explosive-in-the-

univ

14 — © Capital One, 2016

Page 15: You Don't Know Node: Quick Intro to 6 Core Features

SINGLE THREAD - NO WORRIES !

15 — © Capital One, 2016

Page 16: You Don't Know Node: Quick Intro to 6 Core Features

16 — © Capital One, 2016

Page 17: You Don't Know Node: Quick Intro to 6 Core Features

IT'S STILL POSSIBLE TO WRITE BLOCKING CODE IN

NODE.JS. !17 — © Capital One, 2016

Page 18: You Don't Know Node: Quick Intro to 6 Core Features

BLOCKING NODE.JS CODE// blocking.jsconsole.log('Step: 1')for (var i = 1; i<1000000000; i++) { // This will take 100-1000ms}console.log('Step: 2')

18 — © Capital One, 2016

Page 19: You Don't Know Node: Quick Intro to 6 Core Features

BLOCKING NODE.JS CODEvar fs = require('fs')

var contents = fs.readFileSync('accounts.txt','utf8')console.log(contents)console.log('Hello Ruby\n')

var contents = fs.readFileSync('ips.txt','utf8')console.log(contents)console.log('Hello Node!')//data1->Hello Ruby->data2->Hello NODE!

19 — © Capital One, 2016

Page 20: You Don't Know Node: Quick Intro to 6 Core Features

NON-BLOCKING NODE.JS CODEvar fs = require('fs')

var contents = fs.readFile('accounts.txt','utf8', function(err,contents){ console.log(contents)})console.log('Hello Python\n')

var contents = fs.readFile('ips.txt','utf8', function(err,contents){ console.log(contents)})console.log("Hello Node!")//Hello Python->Hello Node->data1->data2

20 — © Capital One, 2016

Page 21: You Don't Know Node: Quick Intro to 6 Core Features

MOST OF NODE IS JAVASCRIPT

> Array> String

> Primitives> Functions> Objects

21 — © Capital One, 2016

Page 22: You Don't Know Node: Quick Intro to 6 Core Features

NODE != BROWSER

JAVASCRIPT22 — © Capital One, 2016

Page 23: You Don't Know Node: Quick Intro to 6 Core Features

HOW TO CREATE GLOBAL VARIABLES (NO window IN NODE)?

23 — © Capital One, 2016

Page 24: You Don't Know Node: Quick Intro to 6 Core Features

GLOBAL24 — © Capital One, 2016

Page 25: You Don't Know Node: Quick Intro to 6 Core Features

global.__filename

25 — © Capital One, 2016

Page 26: You Don't Know Node: Quick Intro to 6 Core Features

global.__dirname

26 — © Capital One, 2016

Page 27: You Don't Know Node: Quick Intro to 6 Core Features

global.module

27 — © Capital One, 2016

Page 28: You Don't Know Node: Quick Intro to 6 Core Features

global.require()

28 — © Capital One, 2016

Page 29: You Don't Know Node: Quick Intro to 6 Core Features

global.process

29 — © Capital One, 2016

Page 30: You Don't Know Node: Quick Intro to 6 Core Features

> How to access CLI input, OS, platform, memory usage, versions, etc.?

> Where to store passwords?

30 — © Capital One, 2016

Page 31: You Don't Know Node: Quick Intro to 6 Core Features

PROCESS31 — © Capital One, 2016

Page 32: You Don't Know Node: Quick Intro to 6 Core Features

process.pid32 — © Capital One, 2016

Page 33: You Don't Know Node: Quick Intro to 6 Core Features

process.versions

33 — © Capital One, 2016

Page 34: You Don't Know Node: Quick Intro to 6 Core Features

process.arch

34 — © Capital One, 2016

Page 35: You Don't Know Node: Quick Intro to 6 Core Features

process.argv

35 — © Capital One, 2016

Page 36: You Don't Know Node: Quick Intro to 6 Core Features

process.env36 — © Capital One, 2016

Page 37: You Don't Know Node: Quick Intro to 6 Core Features

process.uptime()

37 — © Capital One, 2016

Page 38: You Don't Know Node: Quick Intro to 6 Core Features

process.memoryUsage()

38 — © Capital One, 2016

Page 39: You Don't Know Node: Quick Intro to 6 Core Features

process.cwd()

39 — © Capital One, 2016

Page 40: You Don't Know Node: Quick Intro to 6 Core Features

process.exit()

40 — © Capital One, 2016

Page 41: You Don't Know Node: Quick Intro to 6 Core Features

process.on()

41 — © Capital One, 2016

Page 42: You Don't Know Node: Quick Intro to 6 Core Features

WHO LIKES AND UNDERSTANDS CALLBACKS? !42 — © Capital One, 2016

Page 43: You Don't Know Node: Quick Intro to 6 Core Features

http://callbackhell.comfs.readdir(source, function (err, files) { if (err) { console.log('Error finding files: ' + err) } else { files.forEach(function (filename, fileIndex) { console.log(filename) gm(source + filename).size(function (err, values) { if (err) { console.log('Error identifying file size: ' + err) } else { console.log(filename + ' : ' + values) aspect = (values.width / values.height) widths.forEach(function (width, widthIndex) { height = Math.round(width / aspect) console.log('resizing ' + filename + 'to ' + height + 'x' + height) this.resize(width, height).write(dest + 'w' + width + '_' + filename, function(err) { if (err) console.log('Error writing file: ' + err) }) }.bind(this)) } }) }) }})

43 — © Capital One, 2016

Page 44: You Don't Know Node: Quick Intro to 6 Core Features

CALLBACKS ARE NOT VERY DEVELOPMENTAL

SCALABLE !44 — © Capital One, 2016

Page 45: You Don't Know Node: Quick Intro to 6 Core Features

ME WHEN WORKING WITH DEEPLY NESTED CALLBACKS

45 — © Capital One, 2016

Page 46: You Don't Know Node: Quick Intro to 6 Core Features

EVENTS46 — © Capital One, 2016

Page 47: You Don't Know Node: Quick Intro to 6 Core Features

EVENTS

In node.js an event can be described simply as a string with a corresponding callback.

emitter.on('done', function(results) { console.log('Done: ', results)})

47 — © Capital One, 2016

Page 48: You Don't Know Node: Quick Intro to 6 Core Features

EVENT HANDLING IN NODE USES THE OBSERVER PATTERN

48 — © Capital One, 2016

Page 49: You Don't Know Node: Quick Intro to 6 Core Features

AN EVENT, OR SUBJECT, KEEPS TRACK OF ALL FUNCTIONS THAT ARE

ASSOCIATED WITH IT

49 — © Capital One, 2016

Page 50: You Don't Know Node: Quick Intro to 6 Core Features

THESE ASSOCIATED FUNCTIONS, KNOWN AS OBSERVERS, ARE EXECUTED WHEN

THE GIVEN EVENT IS TRIGGERED

50 — © Capital One, 2016

Page 51: You Don't Know Node: Quick Intro to 6 Core Features

USING EVENT EMITTERS

var events = require('events')var emitter = new events.EventEmitter()

emitter.on('knock', function() { console.log('Who\'s there?')})

emitter.on('knock', function() { console.log('Go away!')})

emitter.emit('knock')51 — © Capital One, 2016

Page 52: You Don't Know Node: Quick Intro to 6 Core Features

INHERITING FROM EVENTEMITTER

// job.jsvar util = require('util')var Job = function Job() { // ... this.process = function() { // ... job.emit('done', { completedOn: new Date() }) }}

util.inherits(Job, require('events').EventEmitter)module.exports = Job

52 — © Capital One, 2016

Page 53: You Don't Know Node: Quick Intro to 6 Core Features

INHERITING FROM EVENTEMITTER

// weekly.jsvar Job = require('./job.js')var job = new Job()

job.on('done', function(details){ console.log('Job was completed at', details.completedOn) job.removeAllListeners()})

job.process()53 — © Capital One, 2016

Page 54: You Don't Know Node: Quick Intro to 6 Core Features

LISTENERS

emitter.listeners(eventName)emitter.on(eventName, listener)emitter.once(eventName, listener)emitter.removeListener(eventName, listener)

54 — © Capital One, 2016

Page 55: You Don't Know Node: Quick Intro to 6 Core Features

PROBLEMS WITH LARGE DATA

> Speed: Too slow> Buffer limit: ~1Gb

55 — © Capital One, 2016

Page 56: You Don't Know Node: Quick Intro to 6 Core Features

STREAMSABSTACTIONS FOR

CONTINUOS CHUNKING OF DATA

56 — © Capital One, 2016

Page 57: You Don't Know Node: Quick Intro to 6 Core Features

NO NEED TO WAIT FOR THE

ENTIRE RESOURCE TO

57 — © Capital One, 2016

Page 58: You Don't Know Node: Quick Intro to 6 Core Features

TYPES OF STREAMS

> Readable> Writable> Duplex

> Transform

58 — © Capital One, 2016

Page 59: You Don't Know Node: Quick Intro to 6 Core Features

STREAMS INHERIT FROM EVENT EMITTER

59 — © Capital One, 2016

Page 60: You Don't Know Node: Quick Intro to 6 Core Features

STREAMS ARE EVERYWHERE!

> HTTP requests and responses> Standard input/output (stdin&stdout)

> File reads and writes

60 — © Capital One, 2016

Page 61: You Don't Know Node: Quick Intro to 6 Core Features

READABLE STREAM EXAMPLEprocess.stdin

Standard input streams contain data going into applications.

61 — © Capital One, 2016

Page 62: You Don't Know Node: Quick Intro to 6 Core Features

THIS IS ACHIEVED VIA A

READ OPERATION.62 — © Capital One, 2016

Page 63: You Don't Know Node: Quick Intro to 6 Core Features

INPUT TYPICALLY

COMES FROM THE KEYBOARD USED TO START 63 — © Capital One, 2016

Page 64: You Don't Know Node: Quick Intro to 6 Core Features

To listen in on data from stdin, use the data and end events:

// stdin.jsprocess.stdin.resume()process.stdin.setEncoding('utf8')

process.stdin.on('data', function (chunk) { console.log('chunk: ', chunk)})

process.stdin.on('end', function () { console.log('--- END ---')})

64 — © Capital One, 2016

Page 65: You Don't Know Node: Quick Intro to 6 Core Features

DEMO$ node stdin.js

65 — © Capital One, 2016

Page 66: You Don't Know Node: Quick Intro to 6 Core Features

NEW INTERFACE read()var readable = getReadableStreamSomehow()readable.on('readable', () => { var chunk while (null !== (chunk = readable.read())) { console.log('got %d bytes of data', chunk.length) }})

66 — © Capital One, 2016

Page 67: You Don't Know Node: Quick Intro to 6 Core Features

WRITABLE STREAM EXAMPLEprocess.stdout

Standard output streams contain data going out of the applications.

67 — © Capital One, 2016

Page 68: You Don't Know Node: Quick Intro to 6 Core Features

THIS IS DONE VIA A WRITE OPERATION.

68 — © Capital One, 2016

Page 69: You Don't Know Node: Quick Intro to 6 Core Features

DATA WRITTEN TO STANDARD

OUTPUT IS VISIBLE ON THE

69 — © Capital One, 2016

Page 70: You Don't Know Node: Quick Intro to 6 Core Features

WRITABLE STREAM

To write to stdout, use the write function:process.stdout.write('A simple message\n')

70 — © Capital One, 2016

Page 71: You Don't Know Node: Quick Intro to 6 Core Features

WHAT ABOUT HTTP?

71 — © Capital One, 2016

Page 72: You Don't Know Node: Quick Intro to 6 Core Features

const http = require('http')var server = http.createServer( (req, res) => { var body = '' req.setEncoding('utf8') req.on('data', (chunk) => { body += chunk }) req.on('end', () => { var data = JSON.parse(body) res.write(typeof data) res.end() })})

server.listen(1337)72 — © Capital One, 2016

Page 73: You Don't Know Node: Quick Intro to 6 Core Features

PIPEvar r = fs.createReadStream('file.txt')var z = zlib.createGzip()var w = fs.createWriteStream('file.txt.gz')r.pipe(z).pipe(w)

73 — © Capital One, 2016

Page 74: You Don't Know Node: Quick Intro to 6 Core Features

WHAT DATA TYPE TO USE FOR BINARY DATA?

74 — © Capital One, 2016

Page 75: You Don't Know Node: Quick Intro to 6 Core Features

BUFFERS

Binary data type, to create:

> new Buffer(size)

> new Buffer(array)

> new Buffer(buffer)

> new Buffer(str[, encoding])

Docs: http://bit.ly/1IeAcZ175 — © Capital One, 2016

Page 76: You Don't Know Node: Quick Intro to 6 Core Features

WORKING WITH BUFFER// buf.jsvar buf = new Buffer(26)for (var i = 0 ; i < 26 ; i++) { buf[i] = i + 97 // 97 is ASCII a}console.log(buf) // <Buffer 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a>console.log(buf.toString('utf8')) // abcdefghijklmnopqrstuvwxyz

76 — © Capital One, 2016

Page 77: You Don't Know Node: Quick Intro to 6 Core Features

BUFFER CONVERTIONbuf.toString('ascii') // outputs: abcdefghijklmnopqrstuvwxyzbuf.toString('ascii', 0, 5) // outputs: abcdebuf.toString('utf8', 0, 5) // outputs: abcdebuf.toString(undefined, 0, 5) // encoding defaults to 'utf8', outputs abcde

77 — © Capital One, 2016

Page 78: You Don't Know Node: Quick Intro to 6 Core Features

REMEMBER FS?

fs.readFile('/etc/passwd', function (err, data) { if (err) return console.error(err) console.log(data)});

data is buffer!

78 — © Capital One, 2016

Page 79: You Don't Know Node: Quick Intro to 6 Core Features

DEMO$ node server-stream

79 — © Capital One, 2016

Page 80: You Don't Know Node: Quick Intro to 6 Core Features

STREAMS AND BUFFER DEMO// server-stream.jsapp.get('/stream', function(req, res) { var stream = fs.createReadStream(largeImagePath) stream.pipe(res)})

$ node server-stream

http://localhost:3000/streamhttp://localhost:3000/non-stream

80 — © Capital One, 2016

Page 81: You Don't Know Node: Quick Intro to 6 Core Features

RESULTS IN DEVTOOLS

/stream responds faster!X-Response-Time~300ms vs. 3-5s

81 — © Capital One, 2016

Page 82: You Don't Know Node: Quick Intro to 6 Core Features

STREAM RESOURCES

https://github.com/substack/stream-adventure$ sudo npm install -g stream-adventure$ stream-adventure

https://github.com/substack/stream-handbook

82 — © Capital One, 2016

Page 83: You Don't Know Node: Quick Intro to 6 Core Features

HOW TO SCALE A SINGLE THREADED SYSTEM?83 — © Capital One, 2016

Page 84: You Don't Know Node: Quick Intro to 6 Core Features

CLUSTER USAGE

> Master: starts workers> Worker: do the job, e.g., HTTP server

Number of processes = number of CPUs

84 — © Capital One, 2016

Page 85: You Don't Know Node: Quick Intro to 6 Core Features

CLUSTERSvar cluster = require('cluster')if (cluster.isMaster) { for (var i = 0; i < numCPUs; i++) { cluster.fork() }} else if (cluster.isWorker) { // your server code})

85 — © Capital One, 2016

Page 86: You Don't Know Node: Quick Intro to 6 Core Features

CLUSTER DEMO

1. Run code/cluster.js with node ($ node cluster.js).

2. Install loadtest with npm: $ npm install -g loadtest

3. Run load testing with: $ loadtest http://localhost:3000 -t 20 —c 10

Press control+c on the server terminal86 — © Capital One, 2016

Page 87: You Don't Know Node: Quick Intro to 6 Core Features

CLUSTER LIBRARIES

> Core cluster: lean and mean> strong-cluster-control (https://github.com/

strongloop/strong-cluster-control), or `$ slc run`: good choice

> pm2 (https://github.com/Unitech/pm2): good choice

87 — © Capital One, 2016

Page 88: You Don't Know Node: Quick Intro to 6 Core Features

PM2

https://github.com/Unitech/pm2

http://pm2.keymetrics.io

Advantages:

> Load-balancer and other features> 0s reload down-time, i.e., forever alive

88 — © Capital One, 2016

Page 89: You Don't Know Node: Quick Intro to 6 Core Features

PM2 DEMO: TYPICAL EXPRESS SERVER

var express = require('express')var port = 3000global.stats = {}console.log('worker (%s) is now listening to http://localhost:%s', process.pid, port)var app = express()app.get('*', function(req, res) { if (!global.stats[process.pid]) global.stats[process.pid] = 1 else global.stats[process.pid] += 1; var l ='cluser ' + process.pid + ' responded \n'; console.log(l, global.stats) res.status(200).send(l)})app.listen(port)

89 — © Capital One, 2016

Page 90: You Don't Know Node: Quick Intro to 6 Core Features

PM2 DEMO

Using server.js:$ pm2 start server.js -i 0

In a new window:$ loadtest http://localhost:3000 -t 20 -c 10$ pm2 list

90 — © Capital One, 2016

Page 91: You Don't Know Node: Quick Intro to 6 Core Features

SPAWN VS FORK VS EXEC

> require('child_process').spawn() - large data, stream, no new V8 instance

> require('child_process').fork() - new V8 instance, multiple workers

> require('child_process').exec() - buffer, async, all the data at once

91 — © Capital One, 2016

Page 92: You Don't Know Node: Quick Intro to 6 Core Features

SPAWN EXAMPLE

fs = require('fs')process = require('child_process')var p = process.spawn('node', 'program.js')p.stdout.on('data', function(data)) { console.log('stdout: ' + data)})

92 — © Capital One, 2016

Page 93: You Don't Know Node: Quick Intro to 6 Core Features

FORK EXAMPLE

fs = require('fs')process = require('child_process')var p = process.fork('program.js')p.stdout.on('data', function(data)) { console.log('stdout: ' + data)})

93 — © Capital One, 2016

Page 94: You Don't Know Node: Quick Intro to 6 Core Features

EXEC EXAMPLEfs = require('fs')process = require('child_process')var p = process.exec('node program.js', function (error, stdout, stderr) { if (error) console.log(error.code)})

94 — © Capital One, 2016

Page 95: You Don't Know Node: Quick Intro to 6 Core Features

HOW TO HANDLE ASYNC ERRORS?

95 — © Capital One, 2016

Page 96: You Don't Know Node: Quick Intro to 6 Core Features

HANDLING ASYNC ERRORS

Event Loop: Async errors are harder to handle/debug, because system loses context of the error. Then,

application crashes.

Try/catch is not good enough.

96 — © Capital One, 2016

Page 97: You Don't Know Node: Quick Intro to 6 Core Features

SYNCHRONOUS ERROR IN NODE

try { throw new Error('Fail!')} catch (e) { console.log('Custom Error: ' + e.message)}

For sync errors try/catch works fine.

97 — © Capital One, 2016

Page 98: You Don't Know Node: Quick Intro to 6 Core Features

ASYNC ERROR EXAMPLE

try { setTimeout(function () { throw new Error('Fail!') }, Math.round(Math.random()*100))} catch (e) { console.log('Custom Error: ' + e.message)}

The app crashes!98 — © Capital One, 2016

Page 99: You Don't Know Node: Quick Intro to 6 Core Features

ME WHEN ASYNC ERROR'S THROWN

99 — © Capital One, 2016

Page 100: You Don't Know Node: Quick Intro to 6 Core Features

ASYNC ERRORS

How to deal with it?

!

100 — © Capital One, 2016

Page 101: You Don't Know Node: Quick Intro to 6 Core Features

BEST PRACTICES FOR ASYNC ERRORS?

> Listen to all “on error” events> Listen to uncaughtException

> Use domain (soft deprecated) or AsyncWrap> Log, log, log & Trace

> Notify (optional)> Exit & Restart the process

101 — © Capital One, 2016

Page 102: You Don't Know Node: Quick Intro to 6 Core Features

ON('ERROR')

Anything that inherits from or creates an instance of the above: Express, LoopBack, Sails, Hapi, etc.

server.on('error', function (err) { console.error(err)})

102 — © Capital One, 2016

Page 103: You Don't Know Node: Quick Intro to 6 Core Features

ON('ERROR') CHAINED METHOD EXAMPLE

var http = require(‘http’)var server = http.createServer(app) .on('error', function(e) { console.log(‘Failed to create server’) console.error(e) process.exit(1) })

103 — © Capital One, 2016

Page 104: You Don't Know Node: Quick Intro to 6 Core Features

ON(‘ERROR’) NAMED VARIABLE EXAMPLE

var req = http.request(options, function(res) { // … processing the response})

req.on('error', function(e) { console.log('problem with request: ' + e.message)})

104 — © Capital One, 2016

Page 105: You Don't Know Node: Quick Intro to 6 Core Features

UNCAUGHTEXCEPTION

uncaughtException is a very crude mechanism for exception handling. An unhandled exception means your application - and by extension Node.js itself - is in an undefined state. Blindly resuming means anything could

happen.

105 — © Capital One, 2016

Page 106: You Don't Know Node: Quick Intro to 6 Core Features

UNCAUGHTEXCEPTION

Always listen to uncaughtException!process.on(‘uncaughtException’, handle)

orprocess.addListener('uncaughtException', handle)

106 — © Capital One, 2016

Page 107: You Don't Know Node: Quick Intro to 6 Core Features

UNCAUGHTEXCEPTION EXPANDED EXAMPLES

process.on('uncaughtException', function (err) { console.error('uncaughtException: ', err.message) console.error(err.stack) process.exit(1)})

orprocess.addListener('uncaughtException', function (err) { console.error('uncaughtException: ', err.message) console.error(err.stack) process.exit(1)

107 — © Capital One, 2016

Page 108: You Don't Know Node: Quick Intro to 6 Core Features

DOMAIN

This module is softly deprecated in 4.0 (most likey will be separate from core module), but there's no alternatives

in core as of now.

108 — © Capital One, 2016

Page 109: You Don't Know Node: Quick Intro to 6 Core Features

DOMAIN EXAMPLE

var domain = require('domain').create()domain.on('error', function(error){ console.log(error)})domain.run(function(){ throw new Error('Failed!')})

109 — © Capital One, 2016

Page 110: You Don't Know Node: Quick Intro to 6 Core Features

DOMAIN WITH ASYNC ERROR DEMO

domain-async.js:var d = require('domain').create()d.on('error', function(e) { console.log('Custom Error: ' + e)})d.run(function() { setTimeout(function () { throw new Error('Failed!') }, Math.round(Math.random()*100))});

110 — © Capital One, 2016

Page 111: You Don't Know Node: Quick Intro to 6 Core Features

C++ ADDONS111 — © Capital One, 2016

Page 112: You Don't Know Node: Quick Intro to 6 Core Features

HOW TO WRITE C/C++ BINDING FOR YOUR IOT,

HARDWARE, DRONE, SMARTDEVICE, ETC.?

112 — © Capital One, 2016

Page 113: You Don't Know Node: Quick Intro to 6 Core Features

NODE AND C++

Create the hello.cc file:#include <node.h>

namespace demo {

using v8::FunctionCallbackInfo;using v8::HandleScope;using v8::Isolate;using v8::Local;using v8::Object;using v8::String;using v8::Value;

113 — © Capital One, 2016

Page 114: You Don't Know Node: Quick Intro to 6 Core Features

NODE AND C++

Create the hello.cc file:void Method(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); args.GetReturnValue().Set(String::NewFromUtf8(isolate, "capital one"));}

void init(Local<Object> exports) { NODE_SET_METHOD(exports, "hello", Method);}

NODE_MODULE(addon, init)

} // namespace demo

114 — © Capital One, 2016

Page 115: You Don't Know Node: Quick Intro to 6 Core Features

CREATING binding.gyp

Create binding.gyp:{ "targets": [ { "target_name": "addon", "sources": [ "hello.cc" ] } ]}

115 — © Capital One, 2016

Page 116: You Don't Know Node: Quick Intro to 6 Core Features

NODE-GYP

$ npm install -g node-gyp

https://github.com/nodejs/node-gyp

116 — © Capital One, 2016

Page 117: You Don't Know Node: Quick Intro to 6 Core Features

CONFIGURING AND BUILDING

$ node-gyp configure$ node-gyp build

Check for compiled .node files in build/Release/

117 — © Capital One, 2016

Page 118: You Don't Know Node: Quick Intro to 6 Core Features

C++ ADDONS EXAMPLES

https://github.com/nodejs/node-addon-examples

118 — © Capital One, 2016

Page 119: You Don't Know Node: Quick Intro to 6 Core Features

INCLUDING ADDON

Create hello.js and include your C++ addon:var addon = require('./build/Release/addon')console.log(addon.hello()) // 'capital one'

Run$ node hello.js

119 — © Capital One, 2016

Page 120: You Don't Know Node: Quick Intro to 6 Core Features

CAPITAL ONE

We use Node a lot!

https://www.youtube.com/watch?v=BJPeLJhv1Ic

120 — © Capital One, 2016

Page 121: You Don't Know Node: Quick Intro to 6 Core Features

30-SECOND SUMMARY

1. Event Emitters2. Streams3. Buffers4. Clusters

5. C++ Addons6. Domain121 — © Capital One, 2016

Page 122: You Don't Know Node: Quick Intro to 6 Core Features

THE END

122 — © Capital One, 2016

Page 123: You Don't Know Node: Quick Intro to 6 Core Features

SLIDES & CODE !https://github.com/azat-co/you-dont-know-node

or

PDF: http://bit.ly/1VJWpQK

123 — © Capital One, 2016

Page 124: You Don't Know Node: Quick Intro to 6 Core Features

Q&A ❓"➡$Twitter: @azat_coEmail: [email protected]

124 — © Capital One, 2016