node. JS Learning Note (i)

Source: Internet
Author: User

1. Callback function

Node is an asynchronous event-driven platform, so we often need to use callback functions in our code.

Cases:

1 setTimeout (function() {2     console.log (' callback is called '); 3 },2000);
2. Standard callback function

The callback function format in node. JS is conventional, it has two parameters, the first argument is err, the second argument is Data,err is the error message, and data is the return.

Cases:

function (err,data) {}
3. Acquiring and using modules

In order to support rapid development, the node platform provides a large number of modules, encapsulating the different functions, then we will get the desired module, we come to introduce. In node, we can use the Require function with the following syntax:

(1) Module reference

var math = require ("math");

(2) Module definition

// math.js       function () {      var sum = 0,            = 0,            = arguments,            =  args.length;        while (i<1) {             + = args[i++]      ;      } return

In another file, when a module is introduced through the Require () method, the defined property or method can be called:

// program.js    var math = require (' math ');     function (val) {    return math.add (val,1);};  

(3) Module identification

Is the argument passed to the Require () method, which must be a string that conforms to the name of the small hump, or. The relative path to the beginning, or the absolute path, or the file name suffix. js.

Learning Documents:

http://nodeapi.ucdok.com/#/api/

Http://nodejs.cn/api

node. JS Learning Note (i)

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.