CommonJs Nodejs and custom module (3)

First, what is CommonJS ?

  CommonJS norms proposed , mainly in order to compensate for the current JavaScript is not standard defects. It's the final
pole goal is: to provide a similar Python , Ruby and Java standard library language.
  CommonJS is modular standard, nodejs is CommonJS (modular) implementation.

 

Two Nodejs of modular

1 core module [http module, url module, Fs module]

2 file module [User form]

 

Three custom modules

 

 

first step:

We can common function js pulled into a single file as a module, this module following default situation 
inside a method or property, is not accessible to the outside. If you want external access to methods or properties inside the module,
it must be inside the module through exports or module.exports exposed property or method.

The second step

In these documents the use of modules, through require the introduction of this module mode, this time you can use the 
properties and methods exposed inside the module

 

Evolution 3.1 custom packages

`Config.js` file code

/ * 
This is the config file config module configuration file 
 * / 
var STR = 'IS the this config' ; 

exports.str = STR; / * block exposure method of a * / 
// module.exports = STR;

 

`Common01.js` file code

/ * 
Use the config file with 
 * / 
the let HTTP = the require ( 'HTTP' ); 
the let config = the require ( './ the config.js'); // input module path 


the let App = http.createServer ( function (REQ, RES) { 
    res.writeHead ( 200 is, { "the Content-the Type": "text / HTML; charset = UTF8" }); 
    res.write ( 'hello NodeJS!' ); 
    the console.log (config); 
    / * 
    not exposure data, the results show: {} 
    exposure results, the results show: {STR: 'the this config IS'} 
    * / 
    res.end (); 
}); 
app.listen ( 8002, "127.0.0.1");


The results show

 

 

 

Exposed objects

`Tools.js` file code

// Tools objects 
var Tools = { 
    the Add: function (X, Y) {
         return X + Y; 
    }, 
    the sayHello: function () {
         return 'Hello, NodeJS!' 
    } 
}; 

// exposure module 
// exports.tools = Tools; 
module.exports = Tools;

 

`Commonjs02.js` file code

/ * 
Use with tools file 
 * / 
/ * summary module exports exposed to the exposure difference * / 
var tools = the require ( './ tools'); / * omitted may .js * / 
the console.log (tools); / / {Tools: {the Add: [Function: the Add], the sayHello: [Function: the sayHello]}} 
the console.log (tools.add (1,2));

 

Summary exports are exposed to the difference between exposure module

1 using exports, console.log (tools) the result is: {tools: {add: [Function: add], sayHello: [Function: sayHello]}}

2 using module, console.log (tools) the result is: {add: [Function: add], sayHello: [Function: sayHello]} 

 

The role of node_modules package

 

 

Results of the

 

 

Commentary:

     If the current directory js not find the specified file, the file will automatically go to find node_modules

 

The same module can be achieved `/ bar / bar.js` Find

 

 

Introducing four modules, advanced embodiment, the use of package.json

In the introduction position required startup module cmd, run the following command file generated package.json

npm init --yes

 

 

 

Use the following

 

 

Flow into the base module:

1 project root directory not found nav.js file;

2 Find node_modules folder for nav.js file not found

3 nav to find the folder, and read package.json file, get the file entry nav.js;

4 found nav.js, successfully imported modules.

 

Guess you like

Origin www.cnblogs.com/angle6-liu/p/11707081.html