Blog2013 ≫ Intro to nodejs

Get node from nodejs.org1 that also has a sample programme on the homepage, a few lines that you can run quickly.

One thing to remember is the programmes just run until they crash, they're meant to run forever. The main thing is that it's asynchronous, which means for each line in your code it's going to start processing the first line, then it's going to start processing the next line whether the first one has finished or not. So most functions have a "callback" parameter, which means "do this, and then when you've finished run this function with the result. So with synchronous javascript you might have:

function getSomethingFromTheDatabase ( ) {
  var someThings = [ ];
  // actually do something here...
  return someThings;
}
var result = getSomethingFromTheDatabase( );
console.log( result );

and that's fine, run the (made up) function getSomethingFromTheDatabase which returns a result, and then you console.log it. But with asynchronous code it would be like this:

function outputSomeStuff ( err, result ) {
  if ( err ) {
    return console.error( err );
  }
  console.log( result );
}
function getSomethingFromTheDatabase ( callback ) {
  var someThings = [ ];
  // actually do something here...
  callback( someThings );
}
getSomethingFromTheDatabase( outputSomeStuff );

So you're passing a reference to the function you want to run to the getSomethingFromTheDatabase function, which it runs when it has finished.

It's quite common to see people passing functions anonymously, so you might see this:

function getSomethingFromTheDatabase ( callback ) {
  var someThings = [ ];
  // actually do something here...
  callback( someThings );
}
getSomethingFromTheDatabase( function ( err, result ) {
  if ( err ) {
    return console.error( err );
  }
  console.log( result );
} );

I prefer declaring the functions though. Also it's common in javascript to declare your function like a variable, so you might see code laid out like this instead:

var outputSomeStuff = function( err, result ) {
  if ( err ) {
    return console.error( err );
  }
  console.log( result );
};
var getSomethingFromTheDatabase = function ( callback ) {
  var someThings = [ ];
  // actually do something here...
  callback( someThings );
};
getSomethingFromTheDatabase( outputSomeStuff );

It will have the same result.

It's convention for all functions that are used as callbacks to reserve the first param for errors. Then you check if there's an error straight away, and if there is pass it back up the stack.

The commonly used framework is expressjs.com2, read their guide3 on building an app. That will introduce you to "require" which you have in nodejs but not regular javascript.

Give me a shout with any questions (or if it looks like I've done anything badly wrong here). My code is at github.com/pauly4, I don't have much public nodejs but my first project datemonkey5 is still there! Full of errors not to copy there though!

nodejs: Javascript (programming language of the internets) on the server side.

js: Programming language of the internets, mostly how I make my living.

⬅️ :: ➡️

Paul Clarke's weblog - I live in A small town. Married and father to 2, I am a full-stack web developr, + I do js / Node, some ruby, other languages etc. I like pubs, parkrun, eating, home automation and other diy jiggery-pokery, history, tree stuff, TV, squirrels, pirates, lego, + time travel.