Murvin Lai
  • Me
  • Career
  • Tech Blog
  • My Projects
  • Open Source
    • Install Node.js
    • Node.JS + MongoDB on AWS
    • Node.js + MongoDB + Mongoose
    • Node Crypto Performance
    • NPM
    • My Node Tips
    • Dev NoTe
    • Req and Res in Node.js
    • Remote HTTP Request
  • Foodies
    • My Menu
  • lO_Ok

Node.js Tips


I have used Node.js (and Mongo) for almost 1.5 yrs (today is June 2012).  I love it but I find that this is not an easy thing to program. 

Here are some of my tips for development on Nodejs.

1) When there is a new version of Node.js, don't upgrade yours node especially for production.  I ran into many times that Node.js itself was ok, but the modules I used are not.  My suggestion is to install / upgrade it with the modules probably 1 week later.  Give the other contributors time to update their modules. :)

2) If you make remote http request calls, make sure you have set your max Socket, and raise the ulimit in your actual server.  Also, remote call will slow down the performance even we are in a async world.  

3) Install Node Inspector for debugging.  It saves me lots of time.  A MUST HAVE!!!

4) node-seq is a good one too.  It not only simplify the code, but also make (or... define) the code block for async.   It is awesome.   Otherwise, you have to use Process.nextTick()  to make your block of code async.  ( "Callback" doesn't make the code async).

5) Express.js can simplify a lot for setting up a node.js server.  However, make sure that you aware of the difference between the new and older version of express.js. 

6) For debugging, when the code break but no response back (or hanging there), most likely you miss a next() .  If you got a pre-mature response and notice that the server is still processing after that, you probably have fired two next() somewhere in your code.  If there is error and the whole server down, most likely you have some input data not handling properly. (e.g. assign var a = req.query2.v where query2 doesn't exist).  You better catch this by using uncaughtException. 

7) When creating a remote HTTPS server (https.createServer) with SSL PEM that has both cert & key in the file, make sure your options for cert and key both specify for the same file.  e.g. 
{
  host: ' ...',
  cert: pemFile,
  key: pemFile
}

8) When you use mongoose and find some docs, it is better and safer to manipulate the docs object AFTER you convert into JSON object, instead of the mongoose mongoose JS object.  i.e. JSON.parse(JSON.stringify(docs));

9) If you deal with Date & Time, make sure you use UTF time instead of local time.  I ran into many problems with local time. It is a general tips, not just for Node.js




Copyright @2012 Murvinlai.com