From someone with few experience in JS, what do you recommend for learning Node.js?
I read a lot in the forum about event driven, non-blocking , async, callb
The basic concepts you need to understand to make progress with Node.js are the idea of events, event emitters, and event listeners.
In Node, most functions you can call are non-blocking. When you call fs.ReadStream(), for example, it returns a ReadableStream object. That object is an EventEmitter, so in order to do anything with the contents of the stream, you need to attach a listener to the object, which is a function that gets called when a particular event occurs.
So something like this works:
var fs=require('fs');
var stream = fs.createReadStream("/var/log/messages", { 'flags':'r' });
stream.addListener('data', function(someData) {
console.log(someData);
});
This reads all of the text from the given file, and writes it to the console. When there is data to read from the stream, your function gets called, and is passed the data from the file.
Interestingly, once there is no more data to read from the file, the script exits. Node only stays running as long as there's a valid event listener attached to an emitter, or another asynchronous callback (like a timer) is active.