Tag Archives: tutorial

Getting started with node.js on Windows

The title is somewhat misleading. As of right now, node.js doesn’t run on Windows. You have to run it on some kind of *nix/BSD based system. But there is a somewhat low footprint way to run it and play around with it on your Windows box.

Step 1 – Download and install VirtualBox orVMWare Player. I chose VirtualBox. It’s free, and supports 64-bit guests.

Step 2 – Download The Turnkey Linux core appliance and unzip it somewhere. This handy little virtual machine is based on Ubuntu and give you a basic command line environment with networking.

Step 3 – Import the Turnkey core appliance into VirtualBox.



Choose the .ovf file in the Turnkey directory you unzipped earlier.


Click next and review the settings, making any changes as you see fit. The defaults should work fine. Then click import.


Once Virtualbox finishes importing the virtual machine, you can start it up.

Assuming your network is configured correctly, the virtual machine will grab an IP from your DHCP server and be ready to go.


Step 4 – At this point you can either SSH into the virtual machine or you can connect using the web shell at the address indicated in the startup screen. Initially you can connect using as the root account with no password. You are almost ready to start installing node.js. First type “apt-get update” at the command line to make sure you have all the latest package information.

Step 5 – Install the developer tools you need to get and build node.js. Node.js isn’t packaged as a binary, you have to build it from source. Luckily it includes it’s dependencies and is pretty easy to build. But first we need to get a compiler. Type “apt-get install build-essential” and hit return. A lot of text will fly past, if it asks you if you want to go ahead press “y”.

Step 6 – Install Git. Now you’ve got a compiler installed, we have to install git so we can fetch node.js from the repository. At the command prompt type “apt-get install git”. Once that is complete, type “apt-get install git-core”.

Step 7 – Clone the node.js Git repository. If you want to put node.js is a specific directory, go ahead and make it then “cd” into the new directory. At the command prompt, type “git clone git://github.com/joyent/node.git“.

Step 8 – configure the source for building. type “cd node” and change into the node directory that Git created. Type “./configure”. You may see a few “fail” messages. Don’t worry about them.

Step 9 – Build node.js. Type” “make” at the command prompt. Get a sandwich or a nice cool drink. It doesn’t take very long, but it’s not very exciting unless the Matrix screensaver is your favorite screen saver.

Step 10 – Install node.js and start build applications. Type “make install” once the build is complete. Once that is complete,you can type “node” at the command prompt and you should see the standard help information fly by.

Building a node module or application is beyond the scope of this short tutorial. I suggest reading up at the Node.js site.

Dealing with comma delimited data in a database column

For various reasons, developers through the years have decided to store lists of data as comma-separated lists. Most programming languages include a split() function that allows you to break apart a list of data using a specified character. T-SQL does not.

I don’t remember where I got this split function from. I know I didn’t write all of it from scratch. But basically what it will do is take a delimited list of data, split it apart, and return a table where each data element is associated with the identity value you pass into the function. This is very useful if you just want to get the values for a single row.

CREATE  function Split
	@list varchar(8000),
	@identityVal int,
	@SplitChar char(1)
RETURNS @RetVal table
	id int,
	Value nvarchar(1000)
	WHILE (Charindex(@SplitChar,@list)>0)	
		Insert Into @RetVal (id,Value)
		Select @identityVal,value = (ltrim(rtrim(Substring(@list,1,charindex(@SplitChar,@list)-1))))
		SET @list = Substring(@List,Charindex(@SplitChar,@list)+len(@SplitChar),len(@list))
	INSERT INTO @RetVal (id,Value)
	SELECT @identityVal, ltrim(rtrim(@list))

So what happens if you are trying to build a view or a report and need to split apart multiple rows. You can iterate over the rows in the table you want to flatten out and call the split function for each row, passing the rows PK into the split function.

create function fnFlattenMyTable
RETURNS @RetVal table 
	PrimaryKeyId int, 
	MyListValue varchar(100)
Declare @PrimaryKeyId int
Declare @MyListColumn varchar(6000)

SET @PrimaryKeyId = (select MIN(PrimaryKeyId ) FROM MyTable)
WHILE @PrimaryKeyId  is not null --main loop through MyTable
	Set @MyListColumn = (select MyListColumn from MyTable where PrimaryKeyId  = @PrimaryKeyId )
	insert into @RetVal(PrimaryKeyId , MyListValue )
	select id, value from [Split](@MyListColumn , @PrimaryKeyId , ',')
	SET @PrimaryKeyId  = (select MIN(PrimaryKeyId ) FROM MyTable where PrimaryKeyId  > @PrimaryKeyId )


This function uses one of my favorite sql tricks. It uses a while loop to avoid having to declare a cursor . The logic is pretty simple, on each loop, select the lowest value of the PK in the table that is higher than the previous PK. That’s the roundabout way of saying, “Select the next highest PK from the table”.

Right now, this function is hard coded to flatten the table “MyTable”, but it could be re-factored into a general use function.

Modern Javascript Development: constructors and objects

JavaScript doesn’t have a “class” keyword or any formal class construct. Instead, functions are 1st class objects. When you write a function, you are essentially writing a constructor for a new object.
Here we are defining a object constructor called “newClass” and assigning some member values.

function newClass() { 
    this.name = "newClass"; 
    this.options = {size:42};

var myInstance = new newClass();

You can also specify arguments for the constructor just like any other function and assign the argument values to the objects members.

function newClassInitialzed(name, options) {
    this.name = name;
    this.options = options;
var anotherInstance = new newClassInitialzed("John", {size:42})

So what happens if you don’t want to pass the name and options to the constructor every time? You can specify defaults for the member variables values using the logical OR ( || ) operator in JavaScript. The logical OR operator has a short-circuit which always returns “true” if the first argument is true.

function defaultArgValues(name, options){
    this.name = name || "unknown";
    this.options = options || {size:42};
var defaultArgsInstance = new defaultArgValues();

Since functions are first class objects, we can pass them as arguments in our constructors as well.