Node Learning Notes 01

Verbatim large column  https://www.dazhuanlan.com/2019/08/26/5d634dce89118/

Determine whether to use a programming language, to a large extent, it depends on the ability to develop command-line process. The Node.js as one of the most popular development tools, how to use it to develop command-line process, the Web developer should master the skills.

Node is a server operating environment of the JavaScript language. The so-called "operating environment" has two meanings: First, the JavaScript language running on the server by Node, in this sense, Node bit like the JavaScript virtual machine; secondly, Node provides a number of tools library, making the JavaScript language to interact with the operating system (such as read write documents, create a new child process), in this sense, Node is a tool for JavaScript library.

The following is my study notes

require

  • For loading in the current module and use other modules, passing the name of a module, a module to export the object is returned
  • Module may use a relative path name (beginning with ./), or an absolute path (/ or C: at the beginning of the letter or the like). Further, the extension .js module name can be omitted.

exports

  • exports the current object is the export object module, the module for deriving public methods and properties .
  • Other modules is obtained when the current module exports the object using the current module require function. Deriving a public method of the following example.
1
2
3
exports.hello = function(){
console.log('hello')
}

module

  • You can access some information about the current module by module object, but most use is to replace the export target for the current module. Exporting objects such as modules by default is a normal object, if you want to change a function, you can use the following ways.

    1
    2
    3
    module.exports = function(){
    console.log('hello')
    }
  • Module Initialization

A JS code module execution only when the block is first used once, and export the object during execution of the initialization module. After that, the cached object is exported reused.

  • The main module

NodeJS passed to initiate the process module to the main module is called by the command line parameters. The main module is responsible for scheduling other modules to complete the whole process work.

  • Binary modules

Although we generally use JS to write a module, but NodeJS also supports C / C ++ to write binary modules. Compiled binary module in addition to the document extension is .node, and use the same JS module. Although binary modules can use all the features of the operating system, with unlimited potential

package

If a document name and the like from the storage position of the module define an inlet, a package.json need to include in the package directory file, and wherein the inlet module path specified

file & directory

Small copy of the document

1
2
3
4
5
6
7
8
9
10
var fs = require('fs');
function copy(src,dst){
fs.writeFileSync(dst,fs.readFileSync(src);
}

function main(argv){
copy(argv[0],argv[1]);
}

main(process.argv.slices(2));

Copy large documents

1
2
3
4
5
6
7
8
9
10
11
12

var fs = require('fs');

function copy(src,dst){
fs.createReadStream(src).pipe(fs.createWriteStream(dst));
}

function main(argv){
copy(argv[0],argv[1]);
}

main(process.argv.slice(2));

Guess you like

Origin www.cnblogs.com/JimmyShen/p/11411775.html