Get started with nodejs and nodejs

Source: Internet
Author: User

Get started with nodejs and nodejs

Preface

If we don't want to learn nodeJs, we will get old... when the HTML5 waves hit us, many of our predecessors started the NodeJs journey, and at that time I was still working on server-side programs.
Later, it turned into a front-end, which is far away from the Echelon. Because I have been working on the server language for a long time, I have only started to learn NodeJs and move forward to the complete front-end.
The plan for learning NodeJs this time is:
① 1-2 weeks to learn basic knowledge
② Develop a simple project in about one week
③ Use NodeJs to develop a set of tools for mobile debugging
④ Packaging (this may be far away)

NodeJs features

① Asynchronous
From File Reading to network requests, NodeJs is implemented asynchronously, and callback functions play an important role. Node is a leading Node in programming models.

② Event Callback
Event Callback makes the program light, but it depends on the programmer. However, the callback function is still difficult to read.

③ Single thread
Node is single-threaded. If it is multi-threaded, the language is too deep. It is annoying to ask a few words about the communication in the process, but there is no deadlock in the thread.
However, there is a problem with performance, because multi-core cannot be used;

Module mechanism/CommonJs

We used to develop servers. If we didn't organize code well, it would be very difficult to maintain the code later, so what MVC and what layer-3 architecture would be available?
Currently, the front-end business logic is moving closer to the back-end one by one. For a single-page application, it has exceeded the back-end program logic.
The continuous increase in page view will result in a surge in js Code. How to manage our front-end code becomes a problem, so requireJs has appeared ......
PS: Nima has a dime relationship with nodeJs ......
Javascript has no modular system, so CommonJs was proposed to provide js with the foundation for developing large-scale applications.

Module reference

If we want to reference a module, such as mathematical computing:

Var math = require ('Math ');

Module Definition

If we want to define our own modules, we can do this.

Copy codeThe Code 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 ID is the parameter passed to require. It must be named after the camper and points to a file path, which is similar to requireJS.

Module implementation

The module implementations in Node are divided into two categories: system-level core modules and user-written file modules.
The core module is translated into a binary file during the compilation process. After the Node process is started, some core modules are directly loaded into the memory (file location, compilation and execution)
File modules need to be dynamically loaded, which is relatively slow
However, once loaded, those files will be cached, And the cached files (compiled files) will be read during the second introduction)
Here, we will compile Html to form a template function (it is really just a function) when using underscore. In fact, this can be used as a cache.
Save the compiled functions before deploying the project and remove the html template file (the optimization effect is unknown)

In node, each module is an object:

Copy codeThe Code is as follows:
Function Module (id, parent ){
This. id = id;
This. exports = {};
// Parent is a keyword and should not be used in disorder
This. parent = parent;
If (parent & parent. children ){
Parent. children. push (this );
}
This. filename = null;
This. loaded = false;
This. children = [];
}

Introduce the last phase of the file module during compilation and execution. After the specific file is located, node creates a module object and loads and compiles the object according to the path.
Each compiled Module caches its file path as an index on Module. _ cache.

Each module file contains three variables: require, exports, and module. However, the variables _ filename _ and _ dirname _ are not defined in the file)
In fact, during the compilation process, Node wraps the content of the javascript file at the beginning and end (equivalent to passing in the window through a user-defined function)

Copy codeThe Code is as follows:
(Function (exports, require, module, _ filename __, _ dirname __){
Var math = require ('Math ');
Exports. area = function (radius ){
Return '';
};
});

In this way, the modules are isolated from each other and will not affect each other. Here, the compilation with underscore is somewhat similar ......

Package and NPM

Node organizes its core modules, so the third-party file modules can be compiled and used in an orderly manner. However, in the third-party modules, the modules and modules are still scattered across different regions.
It cannot be referenced directly between each other. In module outsourcing and NPM, it is a mechanism to establish connections.
PS: Many modules form a package. The concept of this package is similar to that of the java package.

After a package structure is decompressed, several files are generated:
① Package. json description file
② Bin executable binary directory
③ Lib javascript code directory
④ Doc document (none of NIMA)
⑤ Test demo

The above are some of the specifications of the CommonJS package, but we can learn a little bit (beginner), NPM needs to be proficient, with NPM We Can skillfully install the management package

Install dependency packages

Installing dependency packages is a common method:

Npm install express
After execution, the node_modules directory will be created under the current directory, and then the express directory will be created under it ......
PS: express is a popular web development framework on NodeJs. It helps us develop a web application quickly.
After the installation is complete, you can call it:

Copy codeThe Code is as follows:
Var express = require ('express ');

Conclusion

This is a simple end, and the actual process of our project is gradually deepened.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.