node. JS Module Reference

Source: Internet
Author: User

1. Example of a reference to a module

var math = require (' math ');

In the Common.js specification, there is the require () method, which accepts the module identifier, which introduces the API of a module into the current context.

2. Module definition

In the module, the context provides the require () method to introduce an external module. Corresponding to the introduced functionality, the context provides a method or variable for the exports object to be used everywhere in the current module, and it is the only exit anywhere, in the module, there is also a Module object, which represents the modules themselves, and exports is the attribute of the modular. In node, a file is a module in which the method is mounted on the exports object as a zodiac to define the way it is exported:

Math.js

Exports.add = function () {

var sum = 0,

i = 0,

args = arguments,

L = args.length

while (I < 1) {

Sum + = Args[i + +];

};

return sum;

};

In another file, after we introduce the module through the Require () method, we can invoke the defined property or method:,

Program.js

var math = require (' math ');

Exports.increment = function (val) {

Return Math.add (Val, 1);

};

node. JS Module Reference

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.