Node.js – fs.open / fs.writeFile, mode, file permissions, umask, and how they interact

Girl Reading Book
While working with Node.js recently, I ran into a situation with the mode parameter used in several functions; fs.open, fs.appendFile, fs.writeFile, etc. When trying to create a new file and specifying a specific mode parameter to allow user and group writes, I found that the file would get created, however would have the wrong permissions.  After much experimenting, I realized that the Linux server’s umask was getting applied, and preventing the creation of a file with the permissions I wanted.

The work around turned out to be fairly simple, although it does create a slight security hole, and should only be used in cases where you can control access to the application.

I simply wrapped the file create calls with the following code :

oldmask = process.umask(newmask);
fs.open(path, flags, [mode], callback)

This only needs be performed when the open (or other method) will create a new file.  Let me know if you have experienced a similar issue, and if you have found other solutions to resolve.

How to escape Mongo keys using Node.js in a Flash

Dale is Flash Gordon's constant companion in his adventures, as well as his one true love.

Dale is Flash Gordon’s constant companion in his adventures, as well as his one true love.

Don’t be scared, dealing with Mongo is not that difficult, especially with some help from Flash.   While some people know of Mongo as a fictional planet where the comic  and movie serials of Flash Gordon takes place, ruled by a tyrant named Ming the Merciless, who governs with an iron hand, others are familiar with Mongo as a cross-platform document-oriented database system.

Many first-time users of  Mongo quickly find out that it does not allow insertion of keys with a dot (.) or dollar sign ($).  While understandable, this would seem to defeat the purpose of having a free-form document that is not rigidly defined.   Luckily, there is an easy way to address this issue in Node.js.  Below is a simple code snippet that I wrote which will escape the dot (.) or dollar sign ($) in any of your keys.

Just pass the document into the function, and your object will be escaped.  The dot (.) will be replaced with _dot_ and the ampersand (&) will be replaced with _amp_.  If required, you can change these to whatever you need.  If you need to unescape, you can easily swap the key.replace params.  I will leave this as an exercise to be completed by the reader…

function escapeKeys(obj) {
    if (!(Boolean(obj) && typeof obj == 'object'
      && Object.keys(obj).length > 0)) {
        return false;
    Object.keys(obj).forEach(function(key) {
        if (typeof(obj[key]) == 'object') {
        } else {
            if (key.indexOf('.') !== -1) {
                var newkey = key.replace(/\./g, '_dot_');
                obj[newkey] = obj[key];
                delete obj[key];
            if (key.indexOf('$') !== -1) {
                var newkey = key.replace(/\$/g, '_amp_');
                obj[newkey] = obj[key];
                delete obj[key];

    return true;
Syk is ruled by the witch-queen Azura, who is feared by everyone on Mongo.

Syk is ruled by the witch-queen Azura, who is feared by everyone on Mongo.

MongoDB is an open-source document database, and the leading NoSQL database.

MongoDB is an open-source document database, and the leading NoSQL database.


Travel Tag – Old Fashioned License Plate Game for iOS


Travel Tag is an iOS app of the classic license plate game that everyone played as a kid while driving down the road with your parents on a long trip. Travel Tag makes both short and long trips fun and helps to pass the time. Simply identify all 50 state license plates.

Travel Tag displays a map of your current location, and even provides help by showing you locations where license plates were recently spottted. From the Home screen, you have three options from the lower menu to select a state. You can display all states, those remaining to be found states, and states already found.

After you identify a license plate, Travel Tag will store the time and location, and allow you to view it again in the future.

See how quickly you can find all 50 States!

The original idea for this iOS app was my daughters, she thought it would be fun to use her iPad to play the game and keep track of her progress.  Over a couple weeks we put together the app and decided to make it available for free so that others could play with it as well!

iOS Simulator Screen shot Feb 5, 2014, 4.08.29 PMiOS Simulator Screen shot Feb 5, 2014, 4.11.45 PMiOS Simulator Screen shot Feb 5, 2014, 4.11.41 PM

Database Admin Controller for Code Igniter and Ext JS 4

girl working on laptop

I have been using Code Igniter and Ext JS 4 for several months.  In that time, I have created a couple basic templates to display MySQL tables and perform CRUD on those tables.  In addition, I have wanted to create some basic admin screens for the Core Igniter configured databases.  In my configuration, I have multiple databases defined, using Code Igniter’s built-in support for multiple database access.  So, given all of that, I have created a controller that will display all Code Igniter configured databases, list the contained tables, and allow you to display and edit those tables.

Right now the basic template supports simple pagination, as well as Adding, Deleting, and Updating records.  While fairly full-featured, it does not yet handle remote sorting.  In addition, I am sure you will find some minor display issues with data fields that are not your standard integer or varchar.  I will look to address these later.  As an added bonus, the template references Ext JS remotely, so you do not need to install Ext JS 4 for these admin screens to work.  If you are interested in giving Ext JS 4 a try, this is a great way to explore with it.

One feature of this controller is that the panel for the table admin is dynamically generated.  This will allow you to save the created JS file, and modify it to suit your needs.  You can easily create very custom screens from this generated template.

If you would like to see updates, or have any issues, leave me a message here and I will try to address them.

You can download the zip file here.

To install, just add the enclosed files to your controllers and views directory and make sure you have a valid database configured.

The Programmer’s Corner

Many, many years ago I ran a BBS called The Programmer’s Corner.  It provided a place for software developers to hang out and share information, all pre-Internet.  It was a great experience, and I met some great folks while building the platform.   Recently I found my archive, and I have resurrected parts of the site on the domain that I first setup to provide access to the Internet from the BBS.  You can find parts of the original site at The Programmer’s Corner.  I have added over 25K files that were once available, many of them containing source code that is still useful today!  Many of these files have not been available anywhere else.

It has been interesting going back into time and seeing what was state of the art twenty years ago.  I am still restoring files, and hoping that I will soon have more info available.  Check back often!  A full listing of all the files available can be found here.

= does not == === | Learn about === alias The Triple Equals


I think this just may be the most important piece of information for any software developer today.  Whatever you do, don’t forget it.

Although many software developers have been using PHP and Javascript for years, many do not know about ===, also called “The Triple Equals”. In the simplest terms, it means equality without type coercion. In other words, if using the triple equals, the values must be equal in type as well as value.

  • 0==false // true
  • 0===false // false, because they are of a different type
  • 1==”1″ // true, auto type coercion
  • 1===”1″ // false, because they are of a different type

Many developers will first encounter === when using JSLint to test their Javascript. JSLint was written by Douglas Crockford, one of the more outspoken proponents of Javascript and JSON.

JavaScript has both strict and type-converting equality comparison. For strict equality the objects being compared must have the same type and:

  • Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
  • Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another.
  • Two Boolean operands are strictly equal if both are true or both are false.
  • Two objects are strictly equal if they refer to the same Object.
  • Null and Undefined types are == (but not ===).
Many experienced JavaScript and PHP developers will advocate ALWAYS using === and !=== instead of == and !=.  The reasons are obvious, you will never find yourself finding out that:
  • 0 == ” is true
  • false == ‘false’ is false
  • false == ‘0’ is true

and the non-transitive demonstration is as follows:

  • false == undefined is false
  • false == null is false
  • null == undefined is somehow true?