Write Module in Coffee Script for Node

There are a few ways to write a module in Coffee Script for Node. Below are two ways to contrast their usages. Notice how they share data @url and use function @find.

1.

user.coffee
-------------
class User
  constructor: (url) ->
    @url = url

  findOrCreate: (user, callback) ->
    @find user.username, (err, res) ->
    ...

  find: (username, callback) ->
    ...
module.exports.User = User

example.coffee
-------------
user = require('user')
User = new user.User('url')
...
User.findOrCreate u, (err, res) ->
  console.log @url
  ...

2.

user.coffee
-------------
init = (url) ->
  @url = url

find = (username, callback) ->
  ...
findOrCreate = (user, callback) ->
  console.log @url
  @find user.username, (err, res) ->
    ...

module.exports =
  init: init
  findOrCreate: findOrCreate

example.coffee
---------------
user = require 'user'
user.init 'url'
...
user.findOrCreate u, (err, res) ->
  ...
Posted in Node.js | Tagged | Comments Off on Write Module in Coffee Script for Node

Even Amazon’s DPReview.com Has Limit:-)

Posted in General | Tagged , | Comments Off on Even Amazon’s DPReview.com Has Limit:-)

Reset CouchDB Instance

It’s really easy to clean up a CouchDB instance as fresh as new. Just delete the database files and .delete folder in /usr/local/var/lib/couchdb and restart the CouchDB service.

$ sudo /etc/init.d/couchdb stop
$ cd /usr/local/var/lib/couchdb
$ sudo rm -rf * .delete
$ sudo /etc/init.d/couchdb start
Posted in NoSQL, Ubuntu | Tagged | Comments Off on Reset CouchDB Instance

Tunneling via Putty

When you use putty to log in CouchDB server, you can also set up tunneling to access its admin console restricted to localhost.

Here’s the additional configurationĀ over your normal setup.

Posted in Ubuntu | Tagged , , | Comments Off on Tunneling via Putty

Running CouchDB Admin Console Remotely

CouchDB default configuration allows only localhost access to its admin console via Futon or curl.

Without compromising server security and access, you can use ssh to tunnel to your CouchDB.

Below is how to set up the tunnel.

First, make sure that your server /etc/ssh/sshd_config has “PermitTunnel yes”.

Second, in your local Ubuntu desktop, run the following command.

$ ssh -fNg -L 5984:127.0.0.1:5984 user@ip-address

Third, from your local desktop, launch a browser and navigate to http://127.0.0.1:5985/_utils

Posted in Ubuntu | Tagged | Comments Off on Running CouchDB Admin Console Remotely

Another vim resource site

vimninjas.com

Posted in General | Tagged | Comments Off on Another vim resource site

Vimcasts.org

For those who use vi or vim, here’s a nice site to watch useful tricks on using vim.

Posted in General | Tagged | Comments Off on Vimcasts.org

Mount Box.net to Ubuntu

I have this box.net account with 50GB free space unused. As Box supports DAV, we can mount it as backup storage. Below lists the commands to get you there.

$ sudo apt-get install davfs2 #assume you don't have davfs2 installed yet
$ sudo mkdir /media/box.net
$ mount -t davfs https://www.box.net/nav /media/box.net #you will be prompted for username and password
$ ls -l /media/box.net #list your box.net folders
# sudo vi /etc/davfs2/secrets #add below line
  https://www.box.net/nav username password
$ sudo vi /etc/fstab #add below line
  LABEL=BOX.NET https://www.box.net/dav /media/box.net davfs rw,user,auto,_netdev 0 0
Posted in Ubuntu | Tagged , | Comments Off on Mount Box.net to Ubuntu

Add Swap Space in Ubuntu

Many vendors provide no default swap partition in their images. If you use Ubuntu, here’s a quick way to add swap space, which is useful when you may run out of allocated physical memory.

$ swapon -s
$ sudo dd if=/dev/zero of=/swapfile bs=1024 count=512k
$ sudo mkswap /swapfile
$ sudo swapon /swapfile
$ swapon -s
$ sudo vi /etc/fstab # add the line below
   LABEL=SWAP /swapfile none swap sw 0 0
Posted in Ubuntu | Tagged , | Comments Off on Add Swap Space in Ubuntu

Riak vs CouchBase

CouchBase 2.0 went beta last month. Here`s a very comprehensive comparison between the two.

Posted in NoSQL | Tagged , | 1 Comment