Below are some of the main points to be taken into account before programming using node.js:-

  1. Write async code: Use synchronous code only when response time is not the issue and thread blocking is acceptable by the system. As this situation is hardly there, write async code as much as possible.
  2. Require first then code: Node uses require for importing modules which is sync in nature. So it is advised to make require as one of first statements of your app.
  3. Listen to error events: Most of the Node objects extend the event emitter and emit the error event. Therefore, it is advisable to add event listener on error event using on().
  4. Do not forget try catch: Although try catch is useless for async code, all sync code should be wrapped in try catch block. See the example below:
    request.get(‘/accounts’
    , (error, response) => {  
    try {
         data = JSON.parse(response) // sync code
    } catch(error) {
        console.error(error)  }
    });
  5. Do not always trust open source developers: In package.json file, remove ^ before package versions as it allows npm install to pull the latest minor version from npm which may break the app. Instead, one should have versions fixed for a stable app and create publishable npm-shrinkwrap.json using npm shrinkwrap.

Read In Details