Practice of exports and module. exports in nodejs

Source: Internet
Author: User

Practice of exports and module. exports in nodejs
As long as you write your own file module in nodejs, you will inevitably encounter a module. for the use of exports and exports, most of other people's code will use "module. exports = <object/function, etc.> "this is a strange method of concatenation. When asked why, it seems to be in the fog of the cloud. This method is more conservative and preventive. In addition to viewing the source code, this issue can only be verified by writing code. Two module files are written. provider. js generates any type of objects. customer. js returns and outputs The provider object. First case: provider. js, which directly sets any type of objects on exports. Copy the code exports = {name: 'kxh'}/* exports = function () {console. log ('kxh') ;}; exports = 'kxh'; */console. log ('******** provider-module *************'); console. log (module); console. log ('******** provider-exports *************'); console. log (exports); copy the code customer. js var p = require ('. /provider '); console. log ('******** customer-result *************'); console. log (p); execute customer. js result: copy the code ******** provider-module ************ {id: 'D: \ ProgramDemos \ Web \ nodejs \ TestNode \ provider. js', exports :{}, parent: {id :'. ', exports :{}, parent: null, filename: 'd :\\ ProgramDemos \ Web \ nodejs \ TestNode \ customer. js', loaded: false, children: [[Circular], paths: ['d: \ ProgramDemos \ Web \ nodejs \ TestNode \ node_modules ', 'd: \ ProgramDemos \ Web \ nodejs \ node_modules ', 'd: \ ProgramDemos \ Web \ node_modules', 'd: \ ProgramDemos \ node_m Odules ', 'd: \ node_modules']}, filename: 'd: \ ProgramDemos \ Web \ nodejs \ TestNode \ provider. js', loaded: false, children: [], paths: ['d :\\ ProgramDemos \ Web \ nodejs \ TestNode \ node_modules ', 'd: \ ProgramDemos \ Web \ nodejs \ node_modules ', 'd: \ ProgramDemos \ Web \ node_modules', 'd: \ ProgramDemos \ node_modules ', 'd: \ node_modules ']} ******** provider-exports ************ {name: 'kxh'} ******* customer -Result *********** {} copy the code. From the result, setting any type of objects directly to exports will not be returned to the calling module by require. Require returns an empty object of module. exports. Case 2: provider. js, which sets any type of attribute for exports. Copy the code exports. name = {firstName: 'xh', lastName: 'K'}; // exports. name = "kxh";/* exports. printName = function () {console. log ("kxh") ;}; */console. log ('******** provider-module *************'); console. log (module); console. log ('******** provider-exports *************'); console. log (exports); copy the code customer. js unchanged. Execute customer. js result: copy the code ******** provider-module *********** {id: 'd: \ ProgramDemos \ Web \ nodejs \ TestNode \ provider. js', exports: {name: {firstName: 'xh', lastName: 'K'}, parent: {id :'. ', exports :{}, parent: null, filename: 'd :\\ ProgramDemos \ Web \ nodejs \ TestNode \ customer. js', loaded: false, children: [[Circular], paths: ['d: \ ProgramDemos \ Web \ nodejs \ TestNode \ node_modules ', 'd: \ Progr AmDemos \ Web \ nodejs \ node_modules ', 'd :\\ ProgramDemos \ Web \ node_modules', 'd :\\ ProgramDemos \ node_modules ', 'd: \ node_modules ']}, filename: 'd: \ ProgramDemos \ Web \ nodejs \ TestNode \ provider. js', loaded: false, children: [], paths: ['d :\\ ProgramDemos \ Web \ nodejs \ TestNode \ node_modules ', 'd: \ ProgramDemos \ Web \ nodejs \ node_modules ', 'd: \ ProgramDemos \ Web \ node_modules', 'd: \ ProgramDemos \\ Node_modules ', 'd: \ node_modules']} ******** provider-exports *********** {name: {firstName: 'xh', lastName: 'K'} ******** customer-result ************ {name: {firstName: 'xh', lastName: 'K'} copy the code. From the result, set any type of attribute for exports, module. exports are synchronized and can be returned by require. Third case: provider. js, which sets any type of attribute for exports and any type attribute with the same name or different names for module. exports. Copy the code exports. name = {firstName: 'xh', lastName: 'K'}; // exports. name = "kxh";/* exports. printName = function () {console. log ("kxh") ;}; * // module. exports. name = {firstName: 'wf ', lastName: 'Z'}; module. exports. mail = "kxh@kxh.com"; console. log ('******** provider-module *************'); console. log (module); console. log ('******** provider-exports *************'); console. log (exports); copy the code customer. js unchanged. Execute customer. js result: copy the code ******** provider-module *********** {id: 'd: \ ProgramDemos \ Web \ nodejs \ TestNode \ provider. js', exports: {name: {firstName: 'xh', lastName: 'K'}, mail: 'kxh @ kxh.com '}, parent: {id :'. ', exports :{}, parent: null, filename: 'd :\\ ProgramDemos \ Web \ nodejs \ TestNode \ customer. js', loaded: false, children: [[Circular], paths: ['d: \ ProgramDemos \ Web \ nodejs \ TestNode \ node _ Les ', 'd: \ ProgramDemos \ Web \ nodejs \ node_modules', 'd: \ ProgramDemos \ Web \ node_modules ', 'd: \ ProgramDemos \ node_modules ', 'd: \ node_modules']}, filename: 'd: \ ProgramDemos \ Web \ nodejs \ TestNode \ provider. js', loaded: false, children: [], paths: ['d :\\ ProgramDemos \ Web \ nodejs \ TestNode \ node_modules ', 'd: \ ProgramDemos \ Web \ nodejs \ node_modules ', 'd: \ ProgramDemos \ Web \ node_modules', 'D: \ ProgramDemos \ node_modules, 'd: \ node_modules ']} ******** provider-exports *********** {name: {firstName: 'xh', lastName: 'K'}, mail: 'kxh @ kxh.com '} ******** customer-result *********** {name: {firstName: 'xh', lastName: 'K'}, mail: 'kxh @ kxh.com '} copy the code. From the result, if the attributes of different names are set, they are merged to the module. exports and return. If it is an attribute of the same name, require returns all modules. exports. Case 4: provider. js, no matter whether you set the social sample type attribute for exports, you directly set any type object for module. exports. Copy the code exports. name = {firstName: 'xh', lastName: 'K'}; // exports. name = "kxh";/* exports. printName = function () {console. log ("kxh") ;}; * // module. exports = {name: 'kxh'}; module. exports = function () {console. log ('kxh') ;}; console. log ('******** provider-module *************'); console. log (module); console. log ('******** provider-exports *************'); console. log (exports); copy the code customer. js unchanged. Execute customer. js result: copy the code ******** provider-module *********** {id: 'd: \ ProgramDemos \ Web \ nodejs \ TestNode \ provider. js', exports: [Function], parent: {id :'. ', exports :{}, parent: null, filename: 'd :\\ ProgramDemos \ Web \ nodejs \ TestNode \ customer. js', loaded: false, children: [[Circular], paths: ['d: \ ProgramDemos \ Web \ nodejs \ TestNode \ node_modules ', 'd: \ ProgramDemos \ Web \ nodejs \ node_modules ', 'D: \ ProgramDemos \ Web \ node_modules', 'd: \ ProgramDemos \ node_modules ', 'd: \ node_modules']}, filename: 'd: \ ProgramDemos \ Web \ nodejs \ TestNode \ provider. js', loaded: false, children: [], paths: ['d :\\ ProgramDemos \ Web \ nodejs \ TestNode \ node_modules ', 'd: \ ProgramDemos \ Web \ nodejs \ node_modules ', 'd: \ ProgramDemos \ Web \ node_modules', 'd: \ ProgramDemos \ node_modules ', 'd: \ node_modules' ]} ******** Provider-exports *********** {name: {firstName: 'xh', lastName: 'K'} ******** customer-result ************ [Function] copy the code from the result, whether you set the social sample type attribute for exports, it is directly a module. if any type of object is set for exports, require returns the module. exports. From the above four practical results, require returns the module. exports, in module. you can set functions, object instances, and basic type variables on exports. Therefore, it is generally a module. exports can be used as a module everywhere. If you want to use exports as the module return, set an attribute for it, and do not set attributes with the same name on module. exports.

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.