Node.js module is loaded and the use of third-party packages - study notes

A, Node.js modular development

1.1 JavaScript developers malpractice
javaScript two major problems in the use, dependence and file naming conflicts.

Modular development 1.2 software
a feature is a module, multiple modules can form a complete application, pulled a module will not affect the operation of other functions.

1.3 Node.js development in modular specification
Node.js provisions of a JavaScript file is a module, the module internal variables and functions defined by default on the outside can not get
inside the module can use exports member objects to export, import other methods require the use of modules .
For example: in the a.js members export to import use b.js

  • Module members, a.js in the Export Members
// a.js
  // 在模块内部定义变量
 let version = 1.0;
 // 在模块内部定义方法
 const sayHi = name => `您好, ${name}`;
 // 向模块外部导出数据  exports 关键字
 exports.version = version;
 exports.sayHi = sayHi;
  • Import module members
 // b.js
  // 在b.js模块中导入模块a,  require关键字
 let a = require('./b.js');
  // 输出b模块中的version变量
 console.log(a.version);
  // 调用b模块中的sayHi方法 并输出其返回值
 console.log(a.sayHi('我用到了你啦')); 

Here Insert Picture Description

Another way to export 1.4 module members

module.exports.version = version;
module.exports.sayHi = sayHi;

exports is an alias (address reference relation) module.exports and export objects subject to final module.exports

Second, the system module

2.1 What is the system module
API Node Runtime Environment provides Because these API are developed in a modular fashion, so we also known as API Node Runtime Environment provides for the system modules.
Here Insert Picture Description

2.2 operating system module file fs

  • Reading the file contents
    fs.reaFile ( 'file path / file name' [, 'file encoding'], the callback);
  • Write the file content
    fs.writeFile ( 'file path / file name', 'data', the callback);
// 1.通过模块的名字fs对模块进行引用
const fs = require('fs');

// 2.通过模块内部的readFile读取文件内容
fs.readFile('./01.helloworld.js', 'utf8', (err, doc) => {
	// 如果文件读取出错err 是一个对象 包含错误信息
	// 如果文件读取正确 err是 null
	// doc 是文件读取的结果
	console.log(err);   //null
	console.log(doc);
});

fs.writeFile('./demo.txt', '即将要写入的内容', err => {
	if (err != null) {
		console.log(err);
		return;
	}
	console.log('文件内容写入成功');
})
 

2.3 operating system module path Path

  • Why the path splicing

Path separator different operating systems are not uniform

  • Splicing syntax path
    path.join ( 'path', 'path', ...)
 // 导入path模块
 const path = require('path');
  // 路径拼接
 let finialPath = path.join('itcast', 'a', 'b', 'c.css');
  // 输出结果 itcast\a\b\c.css
 console.log(finialPath);

VS 2.4 relative path absolute path
in most cases using an absolute path, relative path because sometimes the opposite is the command-line tool current working directory
when reading files or settings file path will choose an absolute path
to use to get the current file is located _dirname absolute path

 // 导入path模块
const path = require('path');

console.log(__dirname); //_dirname获取当前文件所在的绝对路径
console.log(path.join(__dirname, '01.helloworld.js')) // 路径拼接

Here Insert Picture Description

Third, the third-party modules

3.1 What is a third-party modules
others written, with specific functions, modules, we can directly use third-party modules, namely, because the third-party modules usually consist of multiple files and placed in a folder, so they the name of the package.

3.2 third-party modules exist in two forms
in the form js file exists, the project provides an API interface to achieve specific functions.
A command-line tool form, project development aid

3.3 Obtaining third-party modules

  • npmjs.com: third-party modules for storage and distribution warehouse

  • npm (node package manager): node module manages third-party tools
    to download: npm install the module name
    unloading: npm unintall package module name

  • Global installation and local installation
    command-line tool: global installed
    library file: local installation

3.4 third-party modules nodemon
nodemon is a command-line tool to assist in project development.
In Node.js, each modified file must re-execute the file command line tool, very cumbersome.
When executed automatically save changes, then the node without executing the command, crt + c to exit the command.

  • The procedures
    using npm install nodemon -g download it
    from the command line tool used nodemon command substitution node command file
    Here Insert Picture Description

3.5 third-party modules NRM
NRM (npm Registry Manager): npm Download switcher
npm default Download in foreign and domestic slow download speeds and improve
the use of steps
to use npm install nrm -g download it
queries the available download Home nrm ls
switched npm Download nrm use Download name
Here Insert Picture Description

Published 21 original articles · won praise 3 · Views 322

Guess you like

Origin blog.csdn.net/weixin_43482965/article/details/104763228