node.js

29 Oct

JavaScript, Node.js, and the Flying Wedge Anti-Pattern

in javascript, node.js, programming

The "Flying Wedge" is the name of a military formation in which troops would march into battle in a V-shaped formation. That same V-shaped "formation" unfortunately has a tendency to show itself in JavaScript and CoffeScript. And in the vast majority of cases, it is bad practice.

Code that's easy to read, modify, re-use, and debug is better code. A convention that hampers those goals without offering an overriding benefit is an anti-pattern. That, in a nutshell, is why the flying wedge ought to be avoided.

Here's an explanation of the anti-pattern, along with some suggestions for avoiding it.

28 Oct

Node.js Debugging with the Built-in Debugger

in debugging, javascript, node.js, programming

Node debuggerNode debuggerThe HTTP version of the app worked, but the commandline version did not. What went wrong? It was hard to say. The application simply hung, unresponsive. Try/catch and event handlers didn't find anything wrong, and my typical console.log() approach wasn't cutting it, either. I need to fire up Node.js's command line debugger.

This is a short tutorial explaining how to use the debugger that comes built-in to Node.js. It explains how to use the command line debugger to set breakpoints , step through the code, and analyze the debugging output. By the time you're done reading, you should be able to quickly debug your own code.

The Basics

  • Node has a built-in debugger
  • It can be executed using node debug your_scriptfile.js
  • In your app, you can set breakpoints using debugger;

Now let's look at each step of debugging....

26 Oct

Node.js: Five Things Every PHP Developer Should Know

in javascript, node.js, php, programming

I recently started working on a few Node.js applications. Coming most recently from PHP (and Drupal in particular), I found the transition to Node.js to be surprisingly easy. Pleasurable, in fact. But I had to learn to think differently about a few things.

Below I list the five things I think every PHP developer should know about Node.js.

26 Oct

Node.js: Connection Pools and MongoDB

in javascript, mongodb, node.js

Node.js can interact well with MongoDB using the MongoDB native driver. The driver comes with plenty of documentation. However, one feature that isn't explained clearly is the pooling system.

The MongoDB Node library can create a pool of connections to the database that can then be shared throughout your Node application. That means that you can open a series of connections once (at startup) and then use those connections as needed.

Here are the mechanics:

mongodb = require('mongodb');
 
// Define options. Note poolSize.
var serverOptions = {
  'auto_reconnect': true,
  'poolSize': 5
};
 
// Now create the server, passing our options.
var serv = new mongodb.Server('localhost', 27017, serverOptions);
 
// At this point, there is no connection made to the server.
 
// Create a handle to the Mongo database called 'myDB'.
var dbManager = new mongodb.Db('myDB', serv);
 
// NOW we initialize ALL 5 connections:
dbManager.open(function (error, db) {
  // Do something with the connection.
 
  // Make sure to call db.close() when ALL connections need
  // to be shut down.
  db.close();
});

The salient detail of the above code is that the database connections are not opened until Db.open() is called. At that point, all five (our poolSize is 5) will be opened at once. These will be left open until the database is closed.

Internally, the MongoDB library will manage which requests go to which connections.

Note that the variables dbManager and db above both point to the same object. To really make use of pooling, your application will have to do something more sophisticated than the above. After all, there's no sense in opening five connections, executing a single operation, and then closing all five connections.