This document provides lessons learned from real world experiences with Node.js applications. It discusses the importance of upgrading to newer Node.js versions for security and features. It also emphasizes the importance of error handling, using promises for async flow control, and scaling applications using Docker containers. Debugging and monitoring Node.js applications for performance is also covered.
9. #nodestats for March: Node 6.x hits 48% share, Node 7.x breaks 15%, 0.10 and 0.12 drop
below 10% share for the first time:
https://mobile.twitter.com/i/web/status/838922927735132160
12. Node Release Stages
• CURRENT: new features (and bug fixes and
security patches)
• ACTIVE LTS: bug fixes (and security patches)
• MAINTENANCE: only security patches
13. Docker to test deployment
• Didn’t need to install anything on host
> docker run -it -v $(pwd):/src -p 3000 node:6
root@container:> npm install
root@container:> npm start
57. “The goal isn’t about removing
levels of indentation but rather
writing modular code that is easy to
reason about”
Strongloop Blog
http://strongloop.com/strongblog/node-js-callback-hell-promises-generators/
77. var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
});
} else {
// Workers can share any TCP connection
// In this case it is an HTTP server
http.createServer(function(req, res) {
res.writeHead(200);
res.end("hello worldn");
}).listen(8000);
}
var d = require('domain').create();
d.on('error', function(err){
// handle the error safely
console.log(err);
});
// catch the uncaught errors in this
// asynchronous or synchronous code block
d.run(function(){
// the asynchronous or synchronous code
// that we want to catch thrown errors on
var err = new Error('example');
throw err;
});
getTweetsFor("domenic") // promise-returning function
.then(function (tweets) {
var shortUrls = parseTweetsForUrls(tweets);
var mostRecentShortUrl = shortUrls[0];
return expandUrlUsingTwitterApi(mostRecentShortUrl); // promise-returning function
})
.then(httpGet) // promise-returning function
.then(
function (responseBody) {
console.log("Most recent link text:", responseBody);
},
function (error) {
console.error("Error with the twitterverse:", error);
}
);