Category Archives: Node.js

“Synchronous” MongoDB Calls with Fibers

Here I’m showing an example of converting MongoDB node driver into “synchronous” APIs from callback based asynchronous APIs. We define two new classes to wrap up the native Db and Cursor. Two methods are converted: Db.Open and Cursor.toArray. The “sync” … Continue reading

Posted in JavaScript, Node.js, NoSQL | Tagged , , | Comments Off on “Synchronous” MongoDB Calls with Fibers

Node-Fibers Example

With node-fibers, we can write codes in traditionally synchronous fashion and avoid deep nested callback calls without blocking the Node event loop. Below is an example to generate Fibonacci series. Each run resumes the calculation loop and returns a number … Continue reading

Posted in JavaScript, Node.js | Tagged , | Comments Off on Node-Fibers Example

MongoDB Test2

Using a two-core server generating enough traffics and achived over 8000 ticks/s. mongod runs 65% CPU time.

Posted in Node.js, NoSQL, Ubuntu | Tagged | Comments Off on MongoDB Test2

MongoDB Test 1

Simple test in simulated ticks: single node process had throughput of 2394 ticks per second. mongod runs at 44% CPU usage.

Posted in Node.js, NoSQL, Ubuntu | Tagged | Comments Off on MongoDB Test 1

MongoDB Build

Spent a few hours building a simple MongoDB cluster with Digital Ocean VPS. Three nodes: Primary, Secondary and Arbiter. Primary node is located in New York and Secondary node is in San Francisco. Arbiter is also in New York leveraging … Continue reading

Posted in Node.js, NoSQL, Ubuntu | Tagged | Comments Off on MongoDB Build

Encapsulation in Coffee Script

m.coffee ——— publicMethod1 = ->   @publicMember1 = "public member 1"   @publicMember2 = "public member 2"   console.log ‘publicMethod1***’   console.log ‘*’ + @publicMember1 publicMethod2 = ->   console.log ‘publicMethod2***’   @publicMethod1()   console.log ‘*’ + @publicMember2 class Foo … Continue reading

Posted in Node.js | Tagged | Comments Off on Encapsulation in Coffee Script

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) … Continue reading

Posted in Node.js | Tagged | Comments Off on Write Module in Coffee Script for Node

Function call in CoffeeScript

Here’s a little hassle in CoffeeScript syntax. Calling a function with parameter but without parentheses is fine. Below sample code works. express = require ‘express’ app = express() app.configure ->   app.use express.bodyParser()   app.use express.methodOverride()   app.use app.router app.get … Continue reading

Posted in Node.js | Tagged , | Comments Off on Function call in CoffeeScript

WebSocket with socket.io

Here’s a sample chat using socket.io. You can find the codes here. Live Demo. The style sheet is modified to work on iPhone, Android, Chrome, etc. The server echoes back all messages sent by each user in a browser. Socket.io … Continue reading

Posted in Node.js | Tagged , , , , | Comments Off on WebSocket with socket.io

TODO List with node.js, express, dust, backbone, iscroll & jquery mobile

Here’s a small project to play with a few modules in Node. Backbone provides a simple MVC and it’s possible to share the models and views in both client and server side. It can support one page web app with … Continue reading

Posted in Node.js | Tagged , , , , | Comments Off on TODO List with node.js, express, dust, backbone, iscroll & jquery mobile