Wednesday, 25 April 2018

node

1. node concepts
  • http server + request router + request handler
  • use DI everywhere, pass router/handler as parameter to server
  • no 'return' is used, use async function, 'response' object is passed all the way to handler
  • in node, any async function accepts a callback as the last parameter
  • in node, any callback function accepts an error as the first parameter

2. even loop
  • timer: execute timer callbacks
  • pending callbacks: execute I/O callbacks
  • poll: incoming I/O events and callbacks

3. npm config file
  • ~/.npmrc

4. module.export and require
  • module.export return an object
  • require use the object
      module.exports = function(app, db) {};  // file1.js
      require('./app/routes')(app, {});               // file2.js

5. node/nvm commands
  • node -v
  • nvm --version
  • nvm ls-remote
  • nvm install x.x.x
  • nvm uninstall x.x.x
  • nvm use x.x.x
  • nvm use node                                   //switch to latest
  • nvm list
  • nvm install --lts                                //install latest
  • npm install -d                                   //--loglevel info
  • npm install jsonpath --save-dev      //save it in package.json
  • npm install -D jsonpath                   //-D is same as --save-dev
  • npm init                                           //new node app, generate package.json
  • node hello.js

6. node app config file
  • ./config by default
  • can be overwritten: process.env["NODE_CONFIG_DIR"] = __dirname + "/configDir/";

7. commonly lodash functions
  • .find()

8. returning everything as objects of functions

const retryCounter = () => {
    let retryCounter = 0
    const totalRetry = 10 //config.get('errorHandler.retryTotal')

    return {
        retryTotal: () => {
            return totalRetry
        },
        count: () => {
            return retryCounter
        },
        add: () => {
            retryCounter += 1
        }
    }
}

9. print stack trace
  • console.trace("Here I am!")

10. package-lock.json (normally more important than package.json, but)
  • package.json overrule the package-lock.json if package.json has been updated
  • 'npm install' sometimes does not update package-lock.json, better to use 'npm update' 
  • version in package.json
major.minor.patch
1.0.2
~1.0.2 means to install version 1.0.2 or the latest patch version such as 1.0.4
^1.0.2 means to install version 1.0.2 or the latest minor or patch version such as 1.1.0


11. dependencies vs devDependencies
  • dependencies is installed when using 'npm install' or 'npm install $package'
  • devDependencies is installed when using 'npm install' or 'npm install --save-dev $package'
  • dependencies is needed to run a package
  • devDependencies is need to develop a package



reference

No comments:

Post a Comment