DoraCMS

您现在的位置是:首页>文档内容页

文档详情

nodeJs基础教程系列(4)——CommonJS规范

doramart 2015-07-06 14:32:48 NodeJs224745
CommonJS是服务器端模块的规范,Node.js采用了这个规范。yellow根据CommonJS规范,一个单独的文件就是一个模块。加载模块使用require方法,该方法读取一个文件并执行,最后返回文件内部的exports对象。下面就是一个简单的模块文件example.js。

CommonJS规范

CommonJS是服务器端模块的规范,Node.js采用了这个规范。

yellow根据CommonJS规范,一个单独的文件就是一个模块。加载模块使用require方法,该方法读取一个文件并执行,最后返回文件内部的exports对象。下面就是一个简单的模块文件example.js。

console.log("evaluating example.js");

var invisible = function () {
 console.log("invisible");
}

exports.message = "hi";

exports.say = function () {
 console.log(message);
}

使用require方法,加载example.js。god

var example = require('./example.js');

这时,变量example就对应模块中的exports对象,于是就可以通过这个变量,使用模块提供的各个方法。god

{
 message: "hi",
 say: [Function]
}

thisrequire方法默认读取js文件,所以可以省略js后缀名。

var example = require('./example');

js文件名前面需要加上路径,可以是相对路径(相对于使用require方法的文件),也可以是绝对路径。如果省略路径,node.js会认为,你要加载一个核心模块,或者已经安装在本地 node_modules 目录中的模块。如果加载的是一个目录,node.js会首先寻找该目录中的 package.json 文件,加载该文件 main 属性提到的模块,否则就寻找该目录下的 index.js 文件。 下面的例子是使用一行语句,定义一个最简单的模块。yellow

// addition.js

exports.do = function(a, b){ return a + b };

上面的语句定义了一个加法模块,做法就是在exports对象上定义一个do方法,那就是供外部调用的方法。使用的时候,只要用require函数调用即可。

var add = require('./addition');

add.do(1,2)
// 3

再看一个复杂一点的例子。

// foobar.js

function foobar(){
       this.foo = function(){
               console.log('Hello foo');
       }

       this.bar = function(){
               console.log('Hello bar');
       }
}

exports.foobar = foobar;

调用该模块的方法如下:

var foobar = require('./foobar').foobar,
   test   = new foobar();

test.bar(); // 'Hello bar'

有时,不需要exports返回一个对象,只需要它返回一个函数。这时,就要写成module.exports。

module.exports = function () {
 console.log("hello world")
}

文章评论

取消回复
登录 参与评论

评论列表(