you're reading...

A .NET Developer’s Introduction to node.js – Part 1

Nearly my entire professional career has been devoted to developing on Windows platforms, specifically the .NET framework. But every developer needs to stand up and stretch once in a while and expand their body of knowledge to stay current and limber. Lately, one of the hotspots in the software scene has been node.js, a JavaScript based system for creating highly scalable internet applications. This is the story of a C# .NET developer learning node.js to do something useful.

A took a brief look last night and I’m going to try to use node.js to update my FitBit weight statistics. Node.js is just JavaScript and therefore is not that big of a strech (since I use JavaScript everyday in my job), and that’s a good thing. It gives me some room to grow without going too far out there.

Installing node.js

A Bing search for node.js brought me to where I clicked “Download” and then, since I’m on Windows 7, I clicked “Windows Installer”. The installation was as easy as it gets. There are no options. Just run the .msi and away it goes. At the end of the installation, it says to open a command prompt and type node to get started. I did this and this is what I got:

Great! Now what? Off to to read the documentation.

Someday We’ll All Say ‘Goodbye World’

I knocked out the standard HelloWorld example put forth by the documentation on their synopsis page:

var http =  require('http');

http.createServer(function (request, response) {  response.writeHead(200, {'Content-Type': 'text/plain'});  response.end('Hello World'); }).listen(8124);

console.log('Server running at');

After opening my web browser, I pointed it to, and sure enough, there on the screen are….those words.

Now what? More documentation? Pfft. No. Lets just start writing code and see what happens. It’s on to the npm registry to find some existing code to connect to FitBit. Npm is a package manager for node and it’s the easiest way to get different modules required by your software onto your machine. In .NET speak, think of it as NuGet and a way to get all those external references into your project. I searched for FitBit and find fitbit-js over on GitHub.

Over on the GitHub page, the README says to install it like this:

npm install fitbit-js

And here’s what I got:

Giant fail.

I wasn’t really surprised because on the npm page, fitbit-js was listed as good for node.js version >= 0.2 and < 0.5. Since I had just installed node, the version I have is 0.6.13. So, it looks like the package manager is keeping me from installing it that way. That’s probably good, but a little annoying.

Next, I tried just copying the fitbit_client.js file into the node/node_modules/npm/lib folder.

Then, using the example from the fitbit-js project, I try to see if it works as is.  Nope. Or rather, the test is looking for a module called ‘express’. But I don’t see that anywhere. So time to start looking around. A hint on stackoverflow lets me know that I need to install express via the npm. I did, and, voila, I now had express. (Note, the Appendix 1 of the node.js documentation  suggested a few useful packages that are often used). Let’s try the client again.

Now it can’t find connect. Okay. Npm to rescue again. That works, but I do get a warning — something about node 0.5 again.

Hmm. We’ll see if this works anyways.

At this point, I got a little stuck as the syntax for loading up the fitbit_client module was a little unfamiliar. Rather than digging too deep into it (that’s for another time), I instead decided to fork the repository from GitHub and see if that worked.

I edited the package.json to allow me to remove the < 0.5 version limitation and try ‘npm install’ in the fitbit-js folder. That works. I edit the test.js file in the example folder to include my secret key for my fitbit stuff. However, at this point, there is a slight bug in the fitbit-js example.

Keeping Up With the Nodes

The version of the test.js I forked had the following line of code in the setup:

app = express.createServer(connect.bodyParser(),                            
                           connect.session({secret: 'session'}));

However, when we try to hit the page, this is the error we get:

Error: connect.cookieParser("secret") required for security when using sessions

I’m fairly confident that the original author didn’t post broken code, so I’m assuming that something must have changed in the post 0.5 version timeframe. I just changed the above line to:

app = express.createServer(connect.bodyParser(),

I retried the url and was redirected to the FitBit oAuth page asking me to authorize my account to my app. Now we’re talkin! I agreed and got a PIN number to put back into the node.js app. What? It was supposed to redirect me back to the app I used and go from there!

It turns out, I was missing something in the FitBit application setup. I needed to tell FitBit that my application was a browser based app, and I gave it my callback url for it to call me back. Once that was done, I was given the display URL which I clicked on and was able to retrieve some JSON.

Fork and Pull

I had forked the fitbit-js repository, so I committed my changes to make it work with node.js 0.6.13 and pushed them back up to GitHub and submitted a pull request. This sample doesn’t really do much right now; it only retrieves my FitBit activity for a specific date. In the next entry, we’ll work on putting some data into FitBit.



No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


I'm Ken Stone. I'm an indie developer with a focus on .NET and Windows Phone development. I have a day job, but everything I say is mine alone.

TechEd 2012July 11th, 2012
Let the learning begin.
%d bloggers like this: