The document discusses several pain points experienced with Node.js applications and solutions for resolving them. It covers creating a strong foundation by upgrading to Node.js v5, locking down NPM dependencies, handling errors properly with try/catch blocks and promises, deploying applications using Docker for scaling, addressing security issues, and using tools like debug and profilers to improve performance.
8. https://github.com/nodejs/node/wiki/
API-changes-between-v0.10-and-v4
• The domain module has been scheduled for
deprecation, awaiting an alternative for those
who absolutely need domains.
• fs.exists() is now deprecated. It is suggested to
use either fs.access() or fs.stat(). Please read the
documentation carefully.
• Updated setImmediate() to process the full queue
each turn of the event loop, instead of one per
queue.
9. https://nodejs.org/en/blog/release/v5
.0.0/
• Breaking changes.
• When parsing HTTP, don't add duplicates of the
following headers
• HTTP methods and header names must now conform
to the RFC 2616 "token" rule
• zlib: Decompression now throws on truncated input
• buffer: Removed both 'raw' and 'raws' encoding types
from Buffer, these have been deprecated for a long
time.
10. Docker to test deployment
• Didn’t need to install anything on host
> docker run -it -v $(pwd):/src -p 3000 node:5
root@container:> npm install
root@container:> npm start
53. “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/
73. 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;
});
try {
var files = yield readdir(dir)
} catch (er) {
console.error('something happened whilst reading the directory')
}
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);
}
);
app.post('/users', function *(request) {
var user = new User(request.params);
if (yield user.save()) {
return JSON.stringify(user);
} else {
return 422;
}
});