MVC with express and node.js

It seems this site still gets plenty of visitors, so I’ll try keep it updated with what is going on elsewhere.

Recently I have been working much more with node.js and express. One of the things I really wanted to emulate was an MVC structure like I am used to with CakePHP. There are plenty of MVC frameworks for node out there, many based on express, but I wasn’t happy that I would not be learning enough about node by just using a framework and it’s associated conventions.

So here is my take on a simple setup for Node.js and express MVC.

Remove empty paragraph tags with PHP

One of the bigger issues of Rich Text Editors in Content Management Systems is that even in the hands of the best users they can generate all manner of spurious content.

Here is a quick way ,using regular expressions, to remove any of these unnecessary HTML elements from your output content.  This example removes paragraphs that contain only line breaks, non-breaking spaces or white-space.

This is specifically for <p> tags, but I’m sure you can figure out how to adapt it to your needs.

echo preg_replace("/<p[^>]*>[\s| ]*<\/p>/", '', $your_content);

Session lost in CakePHP after adding components.

Here’s a quick tip in case you are losing sessions on your CakePHP with no apparent reason. Check any components that you are using for whitespace (usually at the end of the file).

I came across this recently whilst helping a client with their existing CakePHP site, when logged in one particular page that called a component was killing $this->Session->read(‘Auth’).

As a bonus, here is a lovely CakePHP shell that you can use to find whitespace.

Reading HTML email from within Mutt.

Mutt is a fantastic terminal based email client that has all of the capabilities of its GUI based equivalents. One thing I always found annoying was being unable to read emails that have been sent as HTML only (ugh). Well, here is a nice fix that let’s you use a terminal based web browser to view the emails as soon as you load them, in mutt, with as much formatting as possible maintained.

  1. First close Mutt down.
  2. Make sure you have the terminal based browser lynx installed (e.g apt-get install lynx)
  3. Create (or edit) a file in your home directory called .mailcap and add the following lines:
    text/html; lynx %s; nametemplate=%s.html
    text/html; lynx -dump %s; nametemplate=%s.html; copiousoutput
  4. Add the following to your .muttrc file:
    auto_view text/html
  5. Restart Mutt

Now you can start enjoying your HTML email from within Mutt.

CakePHP 2 – Hashing passwords before saving.

A quick gotcha here that leverages CakePHP’s inbuilt utilities to hash data before you commit it to the database.

The beauty of this method is that you can still run all your validation checks and then hash the data between validation and saving. Why is that important? Consider you have a rule that says a password should be no more than 15 characters. When you hash it, it would break the rule and your record would never save.

All you have to do is hash your fields in the beforeSave function of your model like this example from a User Model:

public function beforeSave() {
if(!empty($this->data['User']['password'])) {
$this->data['User']['password'] = Security::hash($this->data['User']['password']);
return true;

One import aspect of this is to always return true. If you don’t your record will NEVER save.

Installing mongoDB on Debian Squeeze (and Ubuntu).

There’s no point in me trying to sell mongoDB to you, just go over to the mongoDB site and read up on its excellence.

This is how I got mongoDB working on Debian Squeeze. It should work for other Deb based flavours – I’m thinking Ubuntu. I also have a PHP5x setup so you may need to adapt if you are running earlier versions. I’m also assuming you have git installed to fetch the drivers.

All you have to do follows:

First install the necessary mongo stuff (you need php5-dev also as we need the phpize functionality):

sudo apt-get install mongodb mongo-clients php5-dev

Now fetch the official mongoDB php driver from github:

git clone

Change into the driver directory and run the following commands:

cd mongo-php-driver
sudo make install

Copy the driver to php extension dir. To find your extension dir run:

php -i | grep extension_dir

Armed with the location of your extension dir copy the file:

sudo cp modules/ /path/to/php/extension_dir/

Now create a file to load the driver into php at start up:

sudo vi /etc/php5/conf.d/mongo.ini

with the line:

Now restart apache:

sudo /etc/init.d/apache2 restart

Then start an instance of the mongo server and PHP should be mongo friendly.

If you want to test this go grab a copy of the excellent phpMoAdmin and drop the file anywhere you load it through a browser. It should connect off the bat to mongoDB and you can start creating collections and objects in less time than it takes you to type “CREATE TABLE …”.


Behaviour Driven Development for web developers and their clients.

It sells itself:

Feature: Tutor forums
	As a Tutor
	I want to be able to use the forums
	In order to communicate with other tutors
Scenario: View forums
	Given that I am logged in as a Tutor
	And I click on "Tutor Forums"
	Then I should see a paginated list of "Tutor Forums"
Scenario: Create forum
	Given that I am logged in as a Tutor
	And I am on the page "Tutor Forums"
	And I click on the link Create forum
	And I enter a Forum title
	And I click "create forum"
	Then I should be able to create a new forum.
Scenario: Edit forum
	Given that I am logged in as a Tutor
	And I am viewing a forum that I created
	And The forum was created within the past 15 minutes
	Then I should be able to edit the forum
Scenario: View threads
	Given that I am logged in as a Tutor
	And I am on the page "Tutor Forums"
	And I click on the title of a Forum
	I should see a paginated list of threads for that forum.
Scenario: Read thread
	Given that I am logged in as a Tutor
	And I am viewing a list of forum threads
	And I click the title of a thread
	Then I should see a list of message for that thread.
Scenario: Reply to thread
	Given that I am logged in as a Tutor
	And I am viewing a thread
	And I click "post reply"
	And I enter a reply
	And I click "post reply" button
	Then I should be abñle to reply to a thread
Scenario: Edit reply
	Given that I am logged in as a Tutor
	And I am viewing replies to a thread
	And a reply was posted by me
	And the reply was posted within the last 15 minutes
	Then I should be able to edit the reply.

It’s so easy you and your clients can learn it in minutes, and it saves everyone time and clarifies expectations.

PHP users should check out Behat.