Preface
If we don't learn nodeJs, we will get old... When the HTML5 wave hit, many ancestors started their NodeJs journey, and at that time I was still working on server-side programs.
Later, I changed to the front-end, and the distance between it and the echelon was already very large. Because I knew the server-side language and worked for a long time, I only started to learn NodeJs and move towards the complete front-end.
This time, the plan to learn NodeJs is:
① Learn basic knowledge in 1-2 weeks
② Development of a simple project in about 1 week
③ Use NodeJs to develop a set of tools for mobile debugging
④ Packaging related (this may be far away)
NodeJs Features
① Asynchronous
NodeJs are all completed asynchronously from file reading to network requests, and callback functions play an important role. Node is the leading program model
② Event callback
Event callbacks make the program lightweight, but the specificity depends on the programmer. However, the callback function is actually quite difficult to read.
③ Single threaded
Node is single-threaded. If it is multi-threaded, this language will be deeper. It is very annoying to ask a few questions about communication in the process, but the thread does not have deadlocks or other problems.
But there are problems with performance relatedness, because multi-core cannot be used;
Module mechanism/CommonJs
We used to do server-side development. If we didn’t have good organizational code, it would be very difficult to maintain later, so what MVC and three-layer architecture will be there?
Now the business logic of the front-end is gradually moving closer to the back-end. As for single-page applications, it has exceeded the program logic of the back-end.
The continuous increase in page views will lead to a surge in the amount of js code. How to manage our front-end code well has become a problem, so requireJs appears...
PS: Damn this period has a dime relationship with nodeJs...
JavaScript does not have a modular system, so CommonJs proposed to give js the basis for developing large-scale applications.
Module reference
If we want to reference a module, such as mathematical calculation related:
var math = require('math');
Module definition
If we want to define our own module, we can do this
The code copy is as follows:
exports.add = function () {
return sum;
}
If this function is defined in math, it can be used
math.add();
Module ID
The module identifier is the parameter passed to require. It needs to be named for the camel and points to a file path. It is very similar to requireJS here.
Module implementation
Node module implementation is divided into two categories: one is the system-level core module and the other is the file module written by users.
The core module is translated into a binary file during the compilation process. After the Node process is started, some core modules will be loaded directly into memory (file location, compilation and execution)
The file module needs to be loaded dynamically, and the speed is relatively slow.
However, once loaded, those files will be cached, and the cached files (compiled files) will be read when imported again (compiled files)
Let's talk a little further here. When we use underscore, we will compile Html to form a template function (it is really just a function). In fact, this can be cached.
Save the compiled functions before deploying the project and remove the html template file (I don’t know the optimization effect)
In node, each module is an object:
The code copy is as follows:
function Module(id, parent) {
this.id = id;
this.exports = {};
//parent is a keyword, it should not be used randomly
this.parent = parent;
if (parent && parent.children) {
parent.children.push(this);
}
this.filename = null;
this.loaded = false;
this.children = [];
}
The last stage of introducing the file module during compilation and execution. After locating the specific file, node will create a new module object, and then load and compile according to the path.
Each successfully compiled module will cache its file path as an index on Module._cache
Each module file has three variables: require, exports, and module, but it is not defined in the file (the __filename__ and __dirname__ variables are also).
In fact, during the compilation process, Node wraps the content of the javascript file head and tail (equivalent to passing a custom function into the window)
The code copy is as follows:
(function (exports, require, module, __filename__, __dirname__) {
var math = require('math');
exports.area = function (radius) {
return '';
};
});
In this way, the module is isolated and will not affect each other. It is somewhat similar to the compilation of underscore...
Package and NPM
Node organizes its own core modules, so third-party file modules can be written and used in an orderly manner, but in third-party modules, modules are still hashed in various places.
It cannot be directly referenced between each other. In module outsourcing and NPM are a mechanism to establish connections.
PS: Many modules will form a package. The concept of this package is similar to that of Java package.
After decompression of a package structure, several files will be formed:
① package.json description file
② bin executable binary directory
③ lib javascript code directory
④ doc documentation (dazzling basically does not exist)
⑤ test demo
The above are some of the specifications of the CommonJS package, but we can learn a little (beginners), and NPM needs to be proficient in it. With the help of NPM, we can be proficient in installing management packages.
Install dependency package
Installing dependency packages is a common method:
npm install express
After execution, the node_modules directory will be created in the current directory, and then the express directory will be created below it...
PS: express is a popular web development framework on NodeJs, helping us quickly develop a web application
After the installation is completed, it can be called:
The code copy is as follows:
var express = require('express');
Conclusion
This section is simply over, and the actual combat process of our project will gradually deepen.