Bryce Baril @brycebaril
“Node's goal is to provide an easy way to build scalable network programs”nodejs.org
Actually... Turns out JS is a pretty neat language, and Node.js gives you:
This is the single most difficult thing to get used to when using Node.
printf("one\n");
sleep(2);
printf("two\n");
vs.
console.log("one")
setTimeout(function () {
console.log("two")
}, 2000)
Magic & libuv
All IO operations are handled outside of the vertical process flow.
Vertical process flow?
... is really the same as in the browser.
function greet(who) {
console.log("Hello")
console.log("how")
function sayWho() {
console.log(who + "?")
}
setTimeout(sayWho, 0)
console.log("are")
}
greet("you")
When functions are created, they contain the state context of where they were created.
function greet(who) {
console.log("Hello")
console.log("how")
function sayWho() {
console.log(who + "?")
}
process.nextTick(sayWho)
console.log("are")
}
greet("you")
All of this relies on functions being first class variable types. This is the standard way of passing continuations in Node.
Synchronous code tends to be vertical (top->bottom), and asynchronous: horizontal (left->right).
function greet(who) {
console.log("Hello")
console.log("how")
function sayWho() {
process.nextTick(function () {
console.log("?")
})
console.log(who)
}
process.nextTick(sayWho)
console.log("are")
}
greet("you")
You can write a JavaScript file that will run in either Node or the browser. However, browsers don't have the same features as Node.
Browserify will convert almost *any* Node.js code to be browser compatible. It will polyfill as much as possible with browser analogs.
Node.js finally fulfills the promise of a single-language stack. (Looking at you, GWT...)
Odds are good you already know and use JavaScript.
For typical web-based applications, the IO operations are the bottlenecks.
With Node, there is no need for external systems to scale or parallelize IO bandwidth. (e.g. Apache mod_php mod_perl, etc.)
All the most common stuff you'll need is built delivered right with the language:
Every new technology that comes out incorporates lessons from the successes and failures of its predecessors.
From modulecounts.com
While in some ways this can be a disadvantage it does bring a few great benefits:
If the CPU is pegged, the event loop gets blocked.
If you intentionally block the event loop, the event loop gets blocked.
By Default the V8 JavaScript heap is about 2gb. This can be configured higher, but beware of GC costs.
After that short intro, let's delve into some key concepts.
When I started working with Node I thought I knew JavaScript. Node will help you actually learn it.
Node is single-process
libuv is the back-end that provides the asynchronous to Node.
It uses back-end threads to run the event loop and handle continuations.
Worth mentioning again: Any JavaScript code in a single context is Synchronous.
Node keeps track of "pending" things. Your application won't exit until this number is zero. (Open sockets, timers...)
process.memoryUsage()
{ rss: 10371072,
heapTotal: 7195904,
heapUsed: 2442752 }
The node docs are pretty great.
npm is Node's package manager. It is packaged directly with Node. It is awesome.
The Async nature of Node.js makes Exception Handling an even harder topic than usual.
someAsyncThing(foo, function (err, reply) {
if (err) {
console.log(err)
return // IMPORTANT
}
return doStuff(reply)
})
takesCallback(foo, callback) {
someAsyncThing(foo, function (err, reply) {
if (err) return callback(err)
return doStuff(reply)
})
}
When you're ready, you can check out process.uncaughtException and Domains.
To play along:
Let me know how it goes! @brycebaril