Modular developed to solve js file naming conflicts and document-dependent issues.
Modular software development:
A feature is a template, multiple templates can form a complete application, pulled a module will not affect the application of other features.
For example:
an application module is divided into user management module, article management module, merchandise management module, user management module is divided into user management module to add and delete user management module, merchandise management module is divided into search and add modules. Modules independent of each other, a strong maintainability.
Node.js development of modular specifications
Node.js js a predetermined file into a module, functions and variables defined within the module can not be accessed outside the module, it is necessary within the module exports the object to export, by external module require methods for introduction.
(Note: exports an object, require a method !!!)
Object Export method exports
For example: a.js file code
const version=1;
const hello = name =>`hello,${name}`;
exports.version=version;
exports.hello=hello;
b.js file code:
let b=require('./a.js');
//在外部通过reqiure进行导入,require为一个方法
console.log(b.version);
console.log(b.hello('lala'));
By running powershell final result is:
module.exports Object Export method
With module.exports objects you can also export module.
In the code examples in the above read file a.js:
const version=1;
const hello = name =>`hello,${name}`;
module.exports.version=version;
module.exports.hello=hello;
The difference between the two
exports are module.exports alias, both point to the same object at the same address , if they are different, the final export objects to module.exports prevail.
For example:
a.js code is
const version=1;
const hello = name =>`hello,${name}`;
exports.version=version;
exports.hello=hello;
//exports指向的对象是带有version和hello属性的对象
module.exports={
name:"zhangsan"
}
//module.exports指向的对象是带有name属性的对象
b.js of code
let b=require('./a.js');
console.log(b);
The end result is a
command line call should be to: the Node file name
System Module
It refers to a system module Node API provides the runtime environment, as the API is used to develop a modular fashion, so called system modules.
fs: file operating system module
Import module:
const fs=require('fs');
Read the contents of the file:
For example:
//读取上一级css目录中的base.css文件
//文件编码是可选参数
fs.readFile('../css/base.css','utf-8',(err,doc)=>{
//err为读取过程中的错误信息,doc为文件内容
if(err==null){
console.log(doc);
}
});
:( written to the file content will overwrite the previous content)
such as:
fs.writeFile('./a.js', 'content', (err) => {
if(err==null){
console.log('文件写入成功');
}
else{
console.log('文件写入失败,'+err);
}
});
Before Append file does not overwrite the contents :()
//添加一个flag为a的参数则为追加,flag为w时则为写入
fs.writeFile('./a.js', 'content',{'flag':'a'}, (err) => {
if(err==null){
console.log('文件追加成功');
}
else{
console.log('文件追加失败,'+err);
}
});
path: the path the operating system module
Path splicing
Different operating systems different path separator, path system module may select the appropriate path through the analyzing computer operating system separator, and stitching path.
Path splicing syntax:
path.join('路径','路径',...)
E.g:
//引入path系统模块
const path=require('path');
const finalPath=path.join('public','uploads','avatar');
console.log(finalPath);
Absolute and relative paths
Better to use an absolute path when writing a file path, the file can still be operated when such files are moved.
For example:
__dirname represents the absolute path of the current file
const fs=require('fs');
const path=require('path');
console.log(__dirname);
console.log(path.join(__dirname,'a.js'));
fs.readFile(path.join(__dirname,'a.js'),'utf-8',(err,doc)=>{
if(err==null){
console.log(doc);
}
console.log(err);
});
Third party modules
Third-party modules are written others, the module has a specific function, also known as packets.
Obtaining third-party modules
How to download third-party modules into the command-line download and download .
Website:
domestic Web site address (faster): Click here to download
Download the command line:
use npm (third-party modules management tools) command line download third-party modules:
Download defaults to the current command line position.
Installation is divided into local and global installation Installation (local multi-fingered download third-party modules installed on the site, global multi-fingered download installation at the command line).
npm install 模块名称
Uninstall third-party modules
npm uninstall 模块名称
nodemon third-party modules
nodemon helps run automatically when a file is saved files more convenient.
Installation Commands
npm install nodemon -g //-g表示全局安装
Run the file command:
nodemon 文件名称
Press ctrl + c to exit the current monitoring environment.
nrm third-party modules
nrm: npm Download switcher
Steps for usage:
- Use npm install nrm -g to download it.
- Download the list of available queries LS NRM
* represents the current Download - Switch nrm use Download Name
finalized switched npm download address.