March 19, 2013

Starting with Node.js

So, some people asked me to go through a series on Node.js to get them started on their projects. I forgot that in my previous article, I just talked about it at a high level, and did not go through the installation of Node, so let’s do just that.

First go grab the interrupter for Node.js from NODE.js. This will allow you to run any javascript in command line, with basic node.js apis. One thing to note, if you grab the stand alone executable (assuming for windows) don’t forget to add it to your path variable ;-). Next go out and grab this guy here: NPM. That is npm, it is like apt-get, or nuget, but for node.js packages. Really important to get both of these to get started. Just like node.js, make sure you add npm to your path variable.

Once you have done that, now you can start executing command line arguments like:

 npm install fs 

Whoa Jorden, WTF did you just do!1!? NPM just went out and installed fs to my current project. If you wanted to use it globally, use -g at the end like this:

 npm install -g fs 

Now that we got that out of the way, now we can write some basic node.js javascript :-). That package we downloaded was to read and write to the file system. Let’s try to do some of that:

 var myPath = "blah.csv"; var values = GetCSV(myPath); console.log(values); function GetCSV(filePath){ return fs.readFileSync(filePath, 'utf8'); } 

So, that code reads a csv file, and prints it out to the screen. Next take that code, save it to a file named test.js and run it in the command line like this:

 node test.js 

I think that is it for the easy stuff. I will have to make a new article on a different api later. Write a comment if you want to see something specifically :-).