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

Convert string to int in PHP

Here’s another quick tip.  If you have an integer value stored as a string and want to convert it to an “int” type, this tip is for you.  You might want to use this if you’ve read an integer from a text file and therefore it was read in as a string value, but now you want to do a numeric comparison.  Anyways, on to the tip.  The following code converts a the string “25” to integer format.

Read moreConvert string to int in PHP

Check if a variable is an array in Javascript

Here’s a quick tip. When writing javascript and trying to test if a given variable is an array or not, I originally used the method of using the “typeof” operator. Unfortunately, because an array in Javascript is just another type of object, typeof will return “object” for an array variable.

So… instead, it’s recommended (by the people of the internet) that you use the “toString” method which will return a specific string for Array objects.  The following code demonstrates this:

Read moreCheck if a variable is an array in Javascript

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

PHP ldap_bind gives the error “Can’t contact LDAP server”

Recently I had an issue connecting to an Active Directory LDAP server from within PHP using the standard ldap_connect and ldap_bind functions.  The problem is, the code I was using was working without problems on a Linux based webserver, so I knew it wasn’t a problem with the PHP code itself or the parameters I was passing.  So… I did some reading and found many people experiencing similar problems, but not all of them for the the same reason as me.  I’m going to summarize in this article the three main problems I came across so that hopefully one of the solutions solves your particular problem.

Read morePHP ldap_bind gives the error “Can’t contact LDAP server”

Creating a singleton class in PHP

PHP began as a language which was not designed around object oriented principles, but lately you’d be hard pressed to find a PHP application that does not utilize classes, objects, and other object oriented design concepts.  Now that PHP is big into the OO world, you might be wondering how you can implement some common object oriented design patterns like a “singleton” in PHP.

In this article, I’ll go through how to create a singleton class and also briefly describe when you might want to use a singleton design pattern in your application.

OK, so first, what is a singleton? The singleton design pattern is where you never want to have more than a single (hence the name singleton) instance of a given class.  In normal use of classes and objects, you define a class once and then you create many instances (objects) in your application.  Each instance has its own properties.  For example, if you had a class “Person” with attributes “first_name” and “last_name”.  Each instance of “Person” might have different values for “first_name” and “last_name”.  In a singleton instance, there can never be more than one instance of a given class in the application, ever.  Why would you want this? Lets say you wanted your application to only every have one connection to a database.  In this case, you could create a singleton class called “DatabaseConnection” which would ensure that there would only ever be one database connection in your application.  It also means that you can access that one instance globally, so you don’t have to pass your database connection object between functions because it can be accessed from anywhere.  Here’s some example code which implements a “DatabaseConnection” singleton class.

Read moreCreating a singleton class in PHP

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.

server.post('/my_post',my_post_handler);

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