Handling timezones with daylight savings in moment.js

Moment.js is a fantastic library for managing dates in Node.js or other javascript applications.  If you’re reading this article it probably means you’ve used moment.js before in your application, but if not, definitely give it a try.  This article will focus mainly on how to use timezones in your moment.js javascript application.

Read moreHandling timezones with daylight savings in moment.js

Performing a simple find/select in MongoDB from node.js

Here’s a quick tip for those of you interested in using MongoDB from a node.js application.  Here we provide an example of how to perform a “find” to retrieve documents from a MongoDB collection.  For those of you used to the SQL world, this would be equivalent to a SELECT.  The following code snippet retrieves all documents in the “users” collection which have a field “age” with a value greater than 30… or in simple terms, retrieve all users who are older than 30.

Read morePerforming a simple find/select in MongoDB from node.js

Programmatically running unit tests with nodeunit in node.js

Unit testing and unit testing frameworks are an important part of any significant development project. For our needs, the unit testing framework that turned out to be the best option was nodeunit. However, maybe because we are running on Windows (not linux), it took a little bit to figure out a nice clean way to run the unit tests.

In the end, we wrote our own node.js script to run the unit tests and we thought it might be nice to share that with others to save some time.

First you need to install the package using npm.  As expected, you do this by running:

Read moreProgrammatically running unit tests with nodeunit in node.js

Handling POSTed data in Restify and Node.js

When creating a webservice using Restify, you may want to be able to handle data POSTed to your API.  Have a look at our article about creating a RESTful webservice in node.js for the basics on setting up the server and routing the requests.  In that article you see that you use the following code to route a POST request to your handler function.


OK, so now all POST requests will be sent to the my_post_handler function.  The problem is that the data being POSTed to the server needs to be properly parsed so that it can be easily handled by your script.  This is where the Restify plugin bodyParser comes into play.  The plugin can be used easily by simply adding the following line of code before your post handler.  So setting up your routes would look something like this.

Read moreHandling POSTed data in Restify and Node.js

Creating a RESTful webservice with Node.JS

We wanted to create a scalable REST webservice that could be used to access some backend data in our application. I had heard about Node.js but hadn’t actually used it for anything.  This seemed like the ideal situation to give it a try.  Node.js seems to have been built on the concept of having it run on semi-reliable servers and to operate in a “multi-node” style architecture, which is exactly what we want to do. In this post I’ll show you how to build a RESTful webservice using Node.js.

So… I bought and read the “The Node Beginner Book” (ebook) by Manuel Kiessling which was a very good introduction to node.js.  It also provided a bit of javascript intro and is very relevant to building a RESTful web service as it talks about concepts that can be directly applied.  I’d recommend it for people new to Node.js (but who already have a development background).

Read moreCreating a RESTful webservice with Node.JS