Brief introduction to NODEJS study notes--nodejs

Source: Internet
Author: User

Event-driven, non-blocking I/O

Four I/O: synchronous (synchronous) IO and asynchronous (asynchronous) Io, blocking (blocking) IO and non-blocking (non-blocking) IO

Blocking I/O waits for data to continue, otherwise it will block the user process

Results are obtained immediately after a user request in a non-blocking process (for example, if the data is not ready to return an error)

The Google V8 engine, written in C + + code, implements the fifth edition of the ECMAScript specification, which can be run in all mainstream

Operating system, it can even run on mobile terminals (ARM-based processors such as HTC G7, etc.). V8 was first developed to embed Google's open source chrome, but V8 is a standalone module that can be embedded in your own application, the famous node. JS (an asynchronous server framework that uses JavaScript to write efficient Web servers on the service side ) is based on the V8 engine.

More knowledge of V8: http://www.ibm.com/developerworks/cn/opensource/os-cn-v8engine/

The read file in node is asynchronous I/O, and the Ajax data transfer is the same, the code is as follows:

var fs = require (' FS ');

Fs.readfile ('/path ', function (err, file) {

Console.log ("Read file completion");

});

node is single-threaded, there will be no deadlock and other problems, but there will be a few three shortcomings;

1. Unable to take advantage of multi-core CPUs

2. Error will cause the entire application to quit, the robustness of the application is worth testing

3. Heavy CPU consumption makes it impossible to continue calling asynchronous I/O

COMMONJS specification: To compensate for the lack of standard JavaScript defects

COMMONJS Module Specification:

1. Module reference: var math = require("math");

2. Exporting the module using export

Math.js file

Export.add = function () {

function methods

}

Using require to make calls

Program.js file

var math = require (' math ');

Exports.increment = function (val) {

Return Math.add (val,1)

}

Note that the above code generates a Increament method on the right.

Module objects exist in the modules, representing the module itself. Export is the attribute of module

3. Module identification

The parameters that are passed in the Require

The Introduction module in node is divided into the following three steps: path analysis, file positioning, compilation execution

The module is also cached in node, which caches the edited and executed objects. will be loaded first from the cache.

Four parameters passed by the Require () method when the module is loaded:

1. Core modules, such as Http/fs/path, etc.

2. ... or.. Relative path file module at the beginning

3. Absolute path file module with/start

4. Non-path file modules, such as Custom connect modules

The modules in node are divided into core modules (custom in node) and file modules (user-defined)

Module compilation in Node

Different suffix names are compiled differently. Can be passed Console.log (require.extension); View existing extension load mode

Add custom load suffix name load mode: require.extension[' coffee ']

Definition code for Module object:

function Module (ID, parent) {

this.id= ID;

this.exports= {};

this.parent= parent;

if (parent&& Parent.children) {

Parent.children.push (this);

}

This.filename =null;

This.loaded =false;

This.children =[];

}

Define the compilation method for files with different suffix names:

module._extension['. Json '] =function (module, filename) {...}

Direct assignment The delegate changes the reference of the formal parameter, but cannot change the value of the extraterritorial scope:

var change = function (a) {

A = 100;

Console.log (a);//=>100

};

var a = 10;

Change (a)

Console.log (a);//=>10

So if you want to achieve the effect of require introducing a class, assign a value to the Module.exports object

The bottom layer is the c/c++,c/c++ built-in module belonging to the lowest level module.

Brief introduction to NODEJS study notes--nodejs

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.