My name is
Jurgens du Toit.
Systems Developer.
Problem Solver.
Technology and Solving Problems are my passion. I'm a South African that loves my wife, life, and coding.
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.
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.
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);
});
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.
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();
See parser_email on github for the full source of the examples given.
Some useful sites: