Introduction to the nodejs tutorial

  • 2020-03-30 04:20:29
  • OfStack

preface

If we don't learn nodeJs, we'll be old... When the HTML5 wave hit, many previous generations started the NodeJs journey while I was still working on server-side applications
Later, I moved to the front end, which was far away from the echelon. Because I knew the server-side language and had been working on it for a long time, I started to learn NodeJs until now and moved forward to the complete front end
The plan for learning NodeJs is:
1-2 weeks of basic knowledge
Develop a simple project in about 1 week
Using NodeJs to develop a set of tools for mobile debugging
Packaging related (this may be further away)

NodeJs characteristics

1) asynchronous
From file reading to network request, NodeJs is done asynchronously. Callback functions play an important role and Node is a leader in the programming model

(2) event callback
Event callbacks make programs lighter, but it's up to the programmer to decide what to do. However, the callback function is difficult to read

(3) single thread
Node is single-threaded, and if it's multi-threaded, the language gets a lot deeper, so it's annoying to ask a few questions about the communication in the process, but threads don't have deadlocks, etc
But the performance correlation is problematic because you can't use multicore;

Module mechanism /CommonJs

We used to do server-side development, and if we didn't organize the code well, it would be very difficult to maintain later, so what MVC, what three-tier architecture
And now the front end of the business logic by moving closer to the back end, in terms of single-page applications, has exceeded the back end of the program logic
The continuous increase of page view will lead to the explosion of js code. How to manage our front-end code well has become a problem, so requireJs appeared...
PS: this part has a dime to do with nodeJs.
Javascript is not modular system, so there is CommonJs proposed, so that js has the basis for developing large applications

The module reference

If we want to refer to a module, such as mathematical calculation related:

Var math = the require (" math ");

The module definition

We can do this if we want to define our own module


exports.add = function () {
  return sum;
}

If the function is defined in math, it can be used

Math. The add ();

Module identifier

The module id is the parameter passed to require, and you need to name the hump, pointing to a file path, which is very similar to requireJS

Module implements

Module implementations in Node are divided into two categories: system-level core modules and user-written file modules
The core modules are translated into binary files during the compilation process. After the Node process starts, some of the core modules are directly loaded into memory (file location, compilation and execution).
The file module needs to be loaded dynamically and relatively slowly
But once loaded, those files are cached, and the cache files are read on the second entry.
To go further, using underscore compiles the Html to form a template function (it's really just a function) that can be used for caching
Save the compiled function before deploying the project, remove the HTML template file (optimization effect unknown)

In node, each module is an object:


function Module(id, parent) {
  this.id = id;
  this.exports = {};
  //Parent is the keyword and should not be misused with
  this.parent = parent;
  if (parent && parent.children) {
    parent.children.push(this);
  }
  this.filename = null;
  this.loaded = false;
  this.children = [];
}

The last stage of the file module is introduced when compiling and executing. After locating the specific file, node creates a new module object, which is then loaded and compiled according to the path
Each successfully compiled Module is cached on module._cache as an index

Each module file has require, exports, and module variables, but they are not defined in the file (as are the variable s/s/s/s/s/s/s/s/s/s).
In fact, during compilation, Node wraps the contents of the javascript file first and last (the equivalent of a custom function passed into the window)


(function (exports, require, module, __filename__, __dirname__) {
  var math = require('math');
  exports.area = function (radius) {
    return '';
  };
});

This way, modules are isolated from each other and do not affect each other, which is similar to underscore compilation...

Package and NPM

Node organizes its core modules, so third-party file modules can be written and used in an orderly fashion, but in third-party modules, modules are still hashed everywhere
There is no direct reference to each other. Module outsourcing and NPM are mechanisms for establishing connections
PS: many modules will form a package whose concept is similar to that of a Java package

After a package structure is unzipped, several files are formed:
Package. Json description file
Bin executable binary directory
Lib javascript code directory
Doc document (nima basically does not have)
5. Test the demo

The above are some of the CommonJS package specification things, but we just need to know a little bit about it (for beginners), NPM requires proficiency, with NPM we can be proficient in the installation of management packages

Install dependency package

Installing dependency packages is a common method:

NPM install express
After execution, the node_modules directory is created in the current directory, and then the express directory is created under it...
PS: express is a popular NodeJs web development framework that helps us quickly develop a web application
After the installation is over, it can be called:


var express = require('express');

conclusion

This is a brief end, and the actual combat process of our project is gradually deepened later


Related articles: