Posts

Showing posts from May, 2019

How do you configure environment specific configuration of a nodejs app

1) Using Config module Install in your app directory, and edit the default config file. $ npm install config $ mkdir config $ vi config/default.json {    //  Customer module configs    " Customer " :   {      " dbConfig " :   {        " host " :   " localhost " ,        " port " :   5984 ,        " dbName " :   " customers "      } ,      " credit " :   {        " initialLimit " :   100 ,        //  Set low for development        " initialDays " :   1      }    } } Edit config overrides for production deployment:  $ vi config/pro...

What test framework did you use to test your nodejs applications

1) Mocha - Mocha is a feature-rich JavaScript test framework running on  Node.js  and in the browser, making asynchronous testing  simple  and  fun .  Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases Simple Usage - $ npm install mocha $ mkdir test $ $EDITOR test/test.js # or open with your favorite editor In your editor: var assert = require ( 'assert' ) ; describe ( 'Array' , function ( ) { describe ( '#indexOf()' , function ( ) { it ( 'should return -1 when the value is not present' , function ( ) { assert . equal ( [ 1 , 2 , 3 ] . indexOf ( 4 ) , - 1 ) ; } ) ; } ) ; } ) ; Back in the terminal: $ ./node_modules/mocha/bin/mocha Array #indexOf() ✓ should return -1 when the value is not present 1 passing (9ms) Set up a test script in package.json: "scripts" : { "test" : "m...

Which node logger did you use in your project?

Following are the common logger modules: 1) logger SUMMARY A simple logging library that combines the simple APIs of Ruby's logger.rb and browser-js console.log() USAGE A logger has 5 different levels of logging in a specific order: 'fatal', 'error', 'warn', 'info', 'debug' Each of these log levels has its own method on the logging instance. You can set the maximum log level on a logger at runtime. By default, a logger writes to STDOUT, but given a writeable file path, it will log directly to a file. Instantiation: // node/common.js style var logger = require('./logger').createLogger(); // logs to STDOUT var logger = require('./logger').createLogger('development.log'); // logs to a file 2)  simple-node-logger A simple multi-level logger for console, file, and rolling file appenders. Features include: levels: trace, debug, info, warn, error and fatal levels (plus all and off) flexible ap...

What is the purpose of npm-lock.json file

As mentioned at  https://docs.npmjs.com/files/package-lock.json package-lock.json  is automatically generated for any operations where npm modifies either the  node_modules  tree, or  package.json . It describes the exact tree that was generated, such that subsequent installs are able to generate identical trees, regardless of intermediate dependency updates. This file is intended to be committed into source repositories, and serves various purposes: Describe a single representation of a dependency tree such that teammates, deployments, and continuous integration are guaranteed to install exactly the same dependencies. Provide a facility for users to “time-travel” to previous states of  node_modules  without having to commit the directory itself. To facilitate greater visibility of tree changes through readable source control diffs. And optimize the installation process by allowing npm to skip repeated metadata resolutions for previou...