Getting Started with node.js


Looking for a new challenge, I started a small project in node.js. Here's a few simple tips on how to get going quickly.

It's JavaScipt

Yeah, that's obvious, but sometimes stating the obvious is necessary. Node.js is essentially server side JavaScript. This means that all your JS knowledge and tutorials and manuals will be usefull once you get stuck.

It's Event Driven

Node.js is an event driven framework, which can take some getting used to, but it is very powerful in the long run. You can write straight forward bottom to top code, but then you'll be missing out on the real power of node.js. An example:

//Get the events module
var   events = require('events')
    , fs     = require('fs');

//Create a new event object
var file_emitter = new events.EventEmitter();

//Create a listener for our event object
var newFileListener = file_emitter.on('found', function(file) {
    //Do something to the found file
}

//Check for files in a folder
var files = fs.readdirSync('/some/folder');
files.forEach(function(file) {
    //Emit (Trigger) the "found" event on our event object
    file_emitter.emit('found', dest);
});

Multiple Files

Any good coder knows that putting your code in multiple files is a quick win for organized coded. Here's how to do it in node.js:

//In utils.js
exports.trim = trim;
function trim(string) {
    return string.replace(/^\s*|\s*$/g, '')
}

//In the file you want to use the trim function
var utils = require('./utils')

The .js part of the file is automatically added/detected. This is to include a file in the same folder as the calling file, but you can easily just add the correct path traversals to get to other files. The important part in the file being included is setting the function name in the exports Object.

File I/O

Files are all treated as streams in node.js. This means that you can start acting on the data in a file before node has even finished reading the file.

//Open a file as a Readable stream
stream = fs.ReadStream(file);
stream.setEncoding('ascii');
stream.on('data', function(data) {
    //Do something to the file's data mid stream
});
stream.on('close', function () {
    //Do something to the file once it's finished reading
}

Writing to a file is just as simple

stream = fs.createWriteStream(filename, { 'encoding': 'base64' });
stream.write(data);
stream.end();

Want More?

See parser_email on github for the full source of the examples given.

Some useful sites:

Filed Under

blog comments powered by Disqus
My name is
Jurgens du Toit.
I love web development and anything internet related. This is my home site and playground.

Follow me on Twitter. Now.   Find me on Linked In.
Find me on Facebook. Read my Blog.