1. Node.js modular development
1.1 Drawbacks of JavaScript Development
There are two major problems when using JavaScript. File dependency and naming conflict
. References between files have a sequential relationship. The
later introduced js file defines variables with the same name will overwrite
1.2 Modular development in software
A function is a module, multiple modules can form a complete application, and pulling a module away will not affect the operation of other functions
1.3 Modular development specifications in Node.js
- Node.js stipulates that a JavaScript file is a module, and the variables and functions defined in the module are not available externally by default
- The exports object can be used to export members inside the module, and the require method can be used to import other modules
1.4 Export of module members
1.5 Import of module members
1.6 Another way to export module members
exports is the alias of module.exports (address reference relationship), and the export object is ultimately subject to module.exports
1.7 The difference between the two export methods
2. System Module
2.1 What is a system module as a system module
The APIs provided by the Node operating environment. Because these APIs are developed in a modular manner, we also call the APIs provided by the Node operating environment.
2.2 System module fs file operation
- file file, s:system system, file operating system
const fs = require(‘fs’);
Read file content
fs.readFile('File path, file name'[,'File encoding'], callback);
// 通过模块进行引用
const fs = require('fs');
fs.readFile('../try/reset.css', 'utf-8', (err, doc) => {
if (err == null) {
//如果没错,err为null,那么输出文档内容
console.log(doc);
}
})
Write file operation
fs.writeFile('File path, file name','data', callback);
// 通过模块进行引用
const fs = require('fs');
const content = '<h3>这是我写入的</h3>';
fs.writeFile('./1.txt', content, (err, doc) => {
if (err != null) {
//如果写入错误那么就输出错误
console.log(err);
}
})
fs.readFile('./1.txt', 'utf-8', (err, doc) => {
if (err == null) {
//如果没错,err为null,那么输出文档内容
console.log(doc);
}
})
2.3 System module path path operation
Why do path stitching
- The path separators of different operating systems are not uniform
- /public/uploads/avatar
- On windows / and \
- When on Linux /
Path splicing syntax
// 导入path模块
const path = require('path');
// 路径拼接
const finalPath = path.join('public', 'upload', 'avatar');
// 输出
console.log(finalPath);
3. Third-party modules
3.1 What is a third-party module
Modules written by others and have specific functions that we can directly use are third-party modules. Because third-party modules usually consist of multiple files and are placed in a folder, all are also known as packages.
There are two forms of third-party modules:
- Exist in the form of js file, provide API interface to realize the specific functions of the project
- Exist as a command line tool to assist project development
3.2 Obtaining third-party modules
npm (node package manager): Node's third-party module management tool
- Download: npm install
- Uninstall: npm uninstall package module name
Global installation and local secret transfer
- Command line tool: global installation
- Library file: local installation
3.3 third-party module nodemon
nodemon is a command line tool to assist project development.
In Node.js, every time a file is modified, the file must be re-executed in the command line tool, which is very cumbersome.
Steps for usage
-
Download it with npm install nodemon -g
-
Use the nodemon command instead of the node command to execute the file in the command line tool
-
How to use: nodemon app.js (start monitoring)
-
Stop monitoring: ctrl + c
-
Use help: nodemon -h
3.4 Third-party module nrm
- npm install nrm -g install
- nrm ls View URLs that can be downloaded (the ones with * are currently used URLs)
- nrm use taobao (change to domestic website)
Slow download problem:
Switch download source: npm config set registry https://registry.npm.taobao.org
Then execute npm install -g nrm
3.5 Third-party module Gulp
https://blog.csdn.net/weixin_45773503/article/details/107678139
4 package.json file
4.1 Questions about the node_modules folder
- Too many folders and files are too broken, when we copy the whole project to others, the transmission speed will be very slow
- Complex module dependencies need to be recorded to ensure that the version of the module is consistent with the current one, otherwise it will cause the current project to run an error
4.2 package.json
The project description file records the current project information. For example, the name, version, and author of the project. GitHub address, which third-party modules the current project relies on, etc.
Use npm init -y command to generate
{
"name": "Gulp", //名称
"version": "1.0.0", //版本
"description": "", //描述
"main": "gulpfile.js", //主入口
"dependencies": {
// 项目依赖
"gulp": "^4.0.2",
"gulp-babel": "^8.0.0",
"gulp-csso": "^4.0.1",
"gulp-cli": "^2.3.0",
"gulp-uglify": "^3.0.2",
"gulp-file-include": "^2.2.2",
"gulp-less": "^4.0.1",
"gulp-htmlmin": "^5.0.1"
},
"devDependencies": {
}, // 开发依赖
"scripts": {
// 别名
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [], //用关键字描述
"author": "", //作者
"license": "ISC" //遵循的协议
}
Project dependency
In the project development stage and online operation stage, the third-party package that must be relied on becomes the project dependency
Files downloaded using the npm install package name command will be added to the dependencies field in the package.json file by default
{
"dependencies": {
"jquery": "^3.5.1"
}
}
Development dependency
- Third-party packages that need to be relied upon during the development phase of the project and do not need to be relied upon during the online operation phase are called development dependencies
- Use the npm install package name --save-dev command to add the package to the devDependencies field in the package.json file and download the package
Add --save-dev during installation
{
"devDependencies": {
"gulp": "^3.5.1"
}
}
npm install (install development dependencies, and project dependencies, all (devDependencies, dependencies))
npm install --production (install project dependencies, in dependencies)
Alias use
{
"scripts": {
// 别名
"test": "echo \"Error: no test specified\" && exit 1"
"build": "nodemon app.js" //将后面的命令叫做 “build”
},
}
When executing, you only need to use npm run build to directly execute the nodemon app.js command
4.5 The role of the package-lock.json file
- Lock the package version to ensure that there will be no problems when downloading again due to different package versions
- Speed up the download speed, because the tree structure of the third-party package that the project depends on and the download address of the package have been recorded in the file, only need to download when reinstalling, no additional work is required
5. Module loading mechanism in Node.js
5.1 Module path search
require('./find.js');
require('./find');
- If the require method finds the module according to the module path, if it is a complete path , directly import the module
- If the module suffix is omitted , first look for the js file with the same name, look for the js folder with the same name
- If you find a folder with the same name , look for index.js in the folder
- If a folder does not index.js will go to the current folder package.json file looks entry file main options
- If the specified entry file does not exist or no entry file is specified, an error will be reported and the module has not been found
As shown in the figure above, execute require.js under modulefindRules to find main.js and execute
5.2 When the module has no path and no suffix
require('find');
- node.js will assume it is a system module
- node.js will go to the node_modules folder
- First, see if there is a JS file with that name
- Then see if there is a folder with that name
- If it is a folder, see if there is index.js in it
- If there is no index.js, check the main option in package.json in the folder to determine the module entry file
- Otherwise, an error will be reported
6. Gulp
https://blog.csdn.net/weixin_45773503/article/details/107678139
7. web server
https://blog.csdn.net/weixin_45773503/article/details/107743867
8. Node.js asynchronous programming
https://blog.csdn.net/weixin_45773503/article/details/107877597
9. MongoDB
https://blog.csdn.net/weixin_45773503/article/details/107734303