Node.js

Using variables in a Regex MongoDB query in Node.js

A Blog Post by

I was busy writing a Node.js application with MongoDB integration when I came across this little problem with Regular expressions and MongoDB calls.
The following code will just return nothing:

var search = 'Joe';
db.users.find(name: /^search/);
db.users.find(name: {$regex: /^search/});
db.users.find(name: {$regex: "/^" + search + "/"});

The solution to this little problem is quite simple:

db.users.find(name: new RegExp(search));

I hate little issues like these.. so hope this helps someone else.

Created: Wed, 29/08/2012 - 18:19

Adding your Node.JS app as a service in Ubuntu

A Blog Post by

Adding a Node.js application as a service on your Ubuntu server is pretty easy using Ubuntu's Upstart. Just create a config file at /etc/init/myapp.conf with the following:

#/etc/init/myapp.conf
description "Your app name here"
author "This would be you"

# Start the service after everything loaded
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown

# Automatically restart service
respawn
respawn limit 99 5

script
# Navigate to your app directory
cd /path/to/myapp/

Created: Mon, 20/08/2012 - 18:15