NPM 2010 - Express, socket.io, v0.2.0 2011 - Node.js in production at Uber & Linkedin, NPM 1.0 2012 - Node.js creator Ryan Dahl steps away from Node day-to-day, v0.8.0 2013 - The MEAN Stack, eBay’s First Node.js Application, Node.js Memory Leak at Walmart 2014 - Node.js Advisory Board, IO.js – Evented I/O for V8 Javascript 2015 - IO.js 1.0.0, Node.js Foundation(Joyent, IBM, Microsoft, PayPal, Fidelity, SAP and The Linux Foundation), Node.js and io.js are merging, v4.0 is the new v1.0 2016 - v6.0: 93% support in ES2015 2017 - v7.x: 97% support in ES2015 including async/await
C++ Client (Google Chrome) & Server (Node.js) Compiles JS to machine code Has two compilers JS Code JIT Compiler Inline caches Optimisation compiler Machine Code
event Set/Get max listeners Prepend listeners Count listeners Get all listeners event names Error event - best practice, always assign listener for `error` event https://nodejs.org/api/events.html
and hooks the output to a writable stream(destination) source.pipe(destination) .pipe(destination) returns destination so we can chain multiple .pipe calls together
and hooks the output to a writable stream(destination) source.pipe(destination) .pipe(destination) returns destination so we can chain multiple .pipe calls together a.pipe(b).pipe(c).pipe(d)
and hooks the output to a writable stream(destination) source.pipe(destination) .pipe(destination) returns destination so we can chain multiple .pipe calls together a.pipe(b).pipe(c).pipe(d) kinda like on the commend line
and hooks the output to a writable stream(destination) source.pipe(destination) .pipe(destination) returns destination so we can chain multiple .pipe calls together a.pipe(b).pipe(c).pipe(d) kinda like on the commend line a | b | c | d
and hooks the output to a writable stream(destination) source.pipe(destination) .pipe(destination) returns destination so we can chain multiple .pipe calls together a.pipe(b).pipe(c).pipe(d) kinda like on the commend line a | b | c | d const fs = require('fs') const zlib = require('zlib') const r = fs.createReadStream('data.csv') const z = zlib.createGzip() const w = fs.createWriteStream('data.csv.gz') r.pipe(z).pipe(w)
require('http') const fs = require('fs') const server = http.createServer((req, res) => { fs.readFile(__dirname + '/data.csv', (err, data) => { res.end(data) }) }) server.listen(4433) buffers up the entire file into memory before writing back the result to clients 1. high memory usage 2. bad user experience
require('http') const fs = require('fs') const server = http.createServer((req, res) => { fs.readFile(__dirname + '/data.csv', (err, data) => { res.end(data) }) }) server.listen(4433) buffers up the entire file into memory before writing back the result to clients 1. high memory usage 2. bad user experience const server = http.createServer((req, res) => { const stream = fs.createReadStream(__dirname + '/data.csv') stream.pipe(res) }) better
not accidentally impact other code CommonJS: An agreed upon standard for how code modules should be structured rick.js morty.js const callRick = () => { console.log('Wubba-lubba-dub-dub!') } module.exports = callRick const callRick = require('./rick') callRick()
not accidentally impact other code CommonJS: An agreed upon standard for how code modules should be structured rick.js morty.js const callRick = () => { console.log('Wubba-lubba-dub-dub!') } module.exports = callRick const callRick = require('./rick') callRick() ?
Y 1. If X is a core module, a. return the core module b. STOP 2. If X begins with '/' a. set Y to be the filesystem root 3. If X begins with './' or '/' or '../' a. LOAD_AS_FILE(Y + X) b. LOAD_AS_DIRECTORY(Y + X) 4. LOAD_NODE_MODULES(X, dirname(Y)) 5. THROW "not found" LOAD_AS_FILE(X) 1. If X is a file, load X as JavaScript text. STOP 2. If X.js is a file, load X.js as JavaScript text. STOP 3. If X.json is a file, parse X.json to a JavaScript Object. STOP 4. If X.node is a file, load X.node as binary addon. STOP LOAD_INDEX(X) 1. If X/index.js is a file, load X/index.js as JavaScript text. STOP 2. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP 3. If X/index.node is a file, load X/index.node as binary addon. STOP LOAD_AS_DIRECTORY(X) 1. If X/package.json is a file, a. Parse X/package.json, and look for "main" field. b. let M = X + (json main field) c. LOAD_AS_FILE(M) d. LOAD_INDEX(M) 2. LOAD_INDEX(X) LOAD_NODE_MODULES(X, START) 1. let DIRS=NODE_MODULES_PATHS(START) 2. for each DIR in DIRS: a. LOAD_AS_FILE(DIR/X) b. LOAD_AS_DIRECTORY(DIR/X) NODE_MODULES_PATHS(START) 1. let PARTS = path split(START) 2. let I = count of PARTS - 1 3. let DIRS = [] 4. while I >= 0, a. if PARTS[I] = "node_modules" CONTINUE b. DIR = path join(PARTS[0 .. I] + "node_modules") c. DIRS = DIRS + DIR d. let I = I - 1 5. return DIRS
node by default Package - Code managed and maintain by package management system Package Management System - Software that automates installing and updating packages, deals with what version you have or need, and manages dependencies Dependency - Code that another set of code depends on to function node_modules - A folder in your project root that contains all installed packages and dependencies package.json - A file contains meta data about your app or module. includes the list of dependencies to install from NPM when running `npm install` Yarn - A client built on top of NPM, keeps consistency across all installations by adding yarn.lock file, faster than NPM (comparing to v4)
$ npm install something Install Installs into local node_modules directory In your project root (ex. /Home/myApp) Home / myApp / node_modules / something
$ npm install something Install Installs into local node_modules directory In your project root (ex. /Home/myApp) Home / myApp / node_modules / something $ npm install -g something install globally (use case for executables)
minor patch major minor patch caret(^) tilde(~) - breaking change - backwards compatible new functionality - old functionality deprecated, but operational - large internal refactor - bug fix 4.*.* 4.15.*