Introduction to the Nodejs tutorial _node.js

Source: Internet
Author: User
Tags pack

Objective

If we don't learn nodejs, we will be old ... At the time of the HTML5 wave, many ancestors began their journey to Nodejs, and I was working on server-side programs.
Later turned to the front, and echelon of the distance has been very large, because I will server language, still dry for a long time, so until now began to learn Nodejs, forward to the full front
The plan for this study Nodejs is:
① 1-2 weeks of learning basics
② to develop a simple project around 1 weeks
③ uses Nodejs to develop a set of tools for mobile-side debugging
④ Packing related (this may be a bit far away)

Nodejs Features

① Asynchronous
Read from a file to a network request, Nodejs is done asynchronously, and the callback function plays an important role, and node is the leader in the programming model.

② Event Callback
The event callback makes the program lightweight, but how exactly it depends on the programmer. But the callback function is actually a certain difficulty in reading.

③ Single Thread
Node is single-threaded, if multithreading, the language of the water is deep, ask a few words in the process of communication is very annoying, but the thread is not deadlock and other issues
But there is a problem with performance-related problems, because multi-core is not available;

Module mechanism/COMMONJS

We used to do server-side development, if there is no good organization code, later maintenance is very difficult, so will have what MVC, what three-tier architecture
And now the business logic of the front end is closer to the back end, in terms of single page applications, has exceeded the backend program logic
The increase in Page view will bring the proliferation of JS Code, how good management of our front-end code has become a problem, so Requirejs appeared ...
PS: NI This paragraph and nodejs have a dime relationship wow ...
JavaScript is not a modular system, so there are commonjs, so that JS has developed a large application of the foundation

Module reference

If we're going to refer to a module, such as a mathematical computation-related:

var math = require (' math ');

Module definition

We can do this if we want to define our own modules.

Copy Code code as follows:

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

If this function is defined in math, you can use the

Math.add ();

Module identification

Module identification is passed to the require parameters, need to name the hump, pointing to a file path, here and Requirejs very similar

Module implementation

The module implementation in node is divided into two types, one is the core module of system level, the other is the file module written by the user.
The core module is translated into binary files in the compilation process, and some core modules will be loaded into memory directly after the node process is started (file location, compile execution)
File modules need to be loaded dynamically, with a relatively slow speed
Once loaded, however, those files are cached and the cached files are read two times when they are introduced (compiled files)
Here's a little bit further, in the process of using underscore, we will compile HTML to form the template function (he really is just a function), in fact this can do caching
Save the compiled function before you deploy the project, and remove the HTML template file (the optimization effect is not known)

In node, each module is an object:

Copy Code code as follows:

function Module (ID, parent) {
This.id = ID;
This.exports = {};
Parent is the key word and should not be used indiscriminately
This.parent = parent;
if (parent && Parent.children) {
Parent.children.push (this);
}
This.filename = null;
this.loaded = false;
This.children = [];
}

When compiling and executing the last phase of a file module, when positioned to a specific file, node creates a new module object and then loads and compiles it according to the path
Each successfully compiled module caches its file path as an index on the Module._cache

Each module file has require, exports, module three variables, but is not defined in the file (__filename__, __dirname__ variables)
In fact, during the compilation, node wraps the contents of the JavaScript file (which is equivalent to the custom function passed in window).

Copy Code code as follows:

(function (exports, require, module, __filename__, __dirname__) {
var math = require (' math ');
Exports.area = function (RADIUS) {
Return ";
};
});

In this way, the modules and modules are isolated from each other, here and underscore compilation is somewhat similar ...

Package with 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 and modules are still scattered across
cannot be directly referenced to each other, in module outsourcing and NPM is a mechanism that will be linked up
PS: Many modules will form a package, the concept of the package and the concept of Java package, only # The concept of the assembly should be similar

A package structure is decompressed to form several files:
①package.json description File
②bin Executable Binary Directory
③lib JavaScript code Directory
④doc documentation (no MA Basic)
⑤test Demo

The above are some of the specifications of the COMMONJS package, but we know a little bit about it (beginner), NPM needs to be proficient, with NPM we can skillfully install the Management Pack

Install Dependency Pack

Installing a dependency pack 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 below it ...
Ps:express is a popular web development framework on Nodejs that helps us quickly develop a Web application
After the installation is complete, you can call:

Copy Code code as follows:

var express = require (' Express ');

Conclusion

This is the end of the brief, the actual process of our project is gradually deepened

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.