NODEJS(9)Book Read - hands-on-nodejs.pdf - Module and NPM

NODEJS(9)Book Read - hands-on-nodejs.pdf - Module and NPM

1. Starting up from Node and NPM
NPM - Node Package Manager

Global vs. Local
-g is the difference between this 2 mods. Local is the default mode, and it will keep the modules in node_modules directory.

npm Command
Query and list the modules
>npm ls
>npm ls [filter name]

Install and Remove
>npm install [package name]   or >npm install -g [package name]
>npm rm [package name] or >npm rm -g [package name]

Check the Version from Latest Remote
>npm info express version

View all the info local
>nom view express

2. Understanding Module

CommonJS/AMD are the standard to modular the functions.
2.1. Object Literals
var myObject = {
     variableKey: variableValue,
     functionKey: function() {
     }
}

IIFE Immediately-Invoked Function Expressions
( function(){/*code*/}() );
 
I do not quite understand this, I will leave it for future.

Import the global variable for IIFE
var myModule = (function(JQ) {
     function method1(){
          JQ(“.container”).html(“test”);
     }
     return{
          publicMethod: function(){
               method1();
          }
     };
})(JQuery);
myModule.publicMethod();

Module Export
return the object will do.
var myModule = (function(){
     var module = {}, privateVariable = “design”;
     function privateMethod(){
          //…snip...
     }
     module.publicProperty = “designStudio”;
     module.publicMethod = function (){
          console.log(privateVariable);
     }
     return module;
})();

Expand the Module
var myModule = (function (my){
     my.xxMethod = function (){
          …snip...
     }
})(myModule);

The only concern is that we need to have myModule first.

var myModule = (function(my){
     my.xxMethod = function(){
          …snip...
     }
})(myModule || {});

The other concern is that, we may define myModule in a.js and also in b.js, they may have conflict.
(function(my){
     my.xxMethod = function(){
     }
})(window.myModule = window.myModule || {});

2.2 CommonJS
global method require() it can load the module, after this command, we can call all the functions belongs to that module.
for example>
var math = require(‘math’);
math.add(2, 3);

global variable exports, once one method assign to this variable, it become the public function of that module.
for example>
//math.js
exports.add = function(){
     …snip...
};

2.3 AMD Standard
AMD - Asynchronous Module Definition

2.4 the way node.js do
var module = require(‘module_name’);

This will fetch a module that was installed by nom.

var module = require(“./path/to/module_name”);

This will fetch the module from a path relative to the current file.

2.5. How Node resolves a module path
Core modules ——> Module with complete or relative path (“./“ or “/")——> Load the file ———> try append “.js”———> try append “.node”
———> if appending “/package.json”, try loading the package definition and looking for ‘main’ ———> no “.”, no “/“, then it will try to find them in /node_modules

3. Making Modules
One File Module

Sample to export only one public method as module definition.
lib/index.js
var counter = 0;
var onePrivateMethod = function(){
     return counter;
};

var onePublicMethod = function(){
     console.log("you already called this module " + onePrivateMethod() + " times.");
};

module.exports = onePublicMethod;

index.js
var myModule = require('./sillycat-aes/lib/index');
myModule();

When we directly call myModule(); my understanding it is a immediately execution function.
>node index.js 
you already called this module 0 times.

Exports a JavaScript Object
Actually, an JavaScript Object is a collection of functions and key value pairs, I can export one object as follow>
var counter1 = 0;
var onePublicMethod = function(){
     return 'you already called this function ' + (++counter1) + ' times';
};

var counter2 = 0;
var anotherPublicMethod = function(){
     return 'you already called this function ' + (++counter2) + ' times.';
};

module.exports = {
     functionA: onePublicMethod,
     functionB: anotherPublicMethod
};

That is the file provide us functions and objects. The main app who can call all the methods is as follow:
var myModule = require('./sillycat-aes/lib/index');
console.log(myModule.functionA());
console.log(myModule.functionA());

console.log(myModule.functionB());
Here is the console output>
node index.js you already called this function 1 times you already called this function 2 times you already called this function 1 times.

An aggregating module
Put some other module inside yours, expose their methods.

var moduleA = require(‘./moduleA’);
var moduleB = require(‘./moduleB’);

var myFunc = function(){
     return “doing some stuff”;
}

module.exports = {
     funcA:moduleB.funcA,
     funcB:moduleB.funcB,
     funcC:myFunc
}


References:
The Node. Beginner Book.pdf

Example to Build Web 
http://keystonejs.com/getting-started/
http://yeoman.io/

CommonJS and AMD
http://www.feeldesignstudio.com/2013/09/javascript-module-pattern-basics
http://www.feeldesignstudio.com/2013/09/javascript-module-pattern-commonjs
http://www.feeldesignstudio.com/2013/09/javascript-module-pattern-amd
http://www.feeldesignstudio.com/2013/09/javascript-module-pattern-requirejs
http://www.feeldesignstudio.com/2013/10/javascript-module-pattern-further-reading

How to configure package.json
http://docs.spmjs.org/doc/package
http://blog.uifanr.com/2013/03/13/478
http://blog.csdn.net/zimin1985/article/details/18958741

你可能感兴趣的:(nodejs)