Nodejs学习第9天

作者: _palm | 来源:发表于2016-10-22 16:35 被阅读316次

在Node.js中,有一个简单的模块加载系统,在Node.js的模块中,可以直接和文件通信,不仅仅是*.js文件。比如之前的栗子(同目录下):

 var server = require('./server'),
     router = require('./router'),
     dispatcher = require('./dispatcher');

 var handle = {} ;
 handle['/'] = dispatcher.root ;
 handle['/start'] = dispatcher.start ;
 handle['/upload'] = dispatcher.upload ;
 server.start(handle,router.route) ;

server.js中有导出一个函数#start(object[,....)

var http = require('http'),
    url = require('url') ;

 function start(hander,route) {
  function onRequest(req,res) {
   var pathName = url.parse(req.url).pathname ;
   console.log('request path ',pathName) ;

   var postData = '' ;
   req.setEncoding("utf8");
   req.addListener('data',function(postDataChunk){
    //
    postData = postDataChunk ;
   }) ;

   //end
   req.addListener('end',function(){
    //
    route(pathName,hander,res,postData) ;
   }) ;

   //dispatcher
   //route(pathName,hander,res) ;
  }

  http.createServer(onRequest).listen(8081) ;
  console.log('server has started.') ;
 }

 exports.start = start ;

这里直接使用了 exports这个特殊对象导出函数#start , 将#start 这个函数新增到exports对象上,然后使用require('./server') 方式,Node.js模块系统会帮我们获取到exports这个对象,进而可以使用之前新增(导出)到该对象的属性/函数.

值得注意的是,自定义模块导出的写法需要进行区别,方式一:

module.exports = {
    fun: function(name) {
        console.log('hello, ',name) ;
    }
} ;

call:

var nd = require('./nd') ;
nd.fun('palm') ;
//Print
hello,  palm

这种导出改变了module.exports对象的指向,之前module.exports 为一个空对象, 如:

module.exports = {} ;

方式二:

/*module.exports = {
    fun: function(name) {
        console.log('hello, ',name) ;
    }
} ;*/

exports.fun = function(name){
    console.log('hello, ',name) ;
} ;

这里就是在空对象 - exports上新增一个函数,类似平时编写js时,将相同业务的功能函数归类编写,方便使用,如:

var util = util || {} ;
util.print = function(msg) {
    if (console.log) {
        console.log(' pring -- ',msg) ;
    }
} ;

不太一样的是,上面需要使用var 声明变量,而exports不用我们声明,因为这是Node.js内置对象.

方式三:

/*module.exports = {
    fun: function(name) {
        console.log('hello, ',name) ;
    }
} ;*/

function fn(name){
    console.log('hello, ',name) ;
} ;

exports.fun = fn ;

这种方式其实和方式二一毛一样,只是个人爱好而已,有些人喜欢事情一件一件的做,什么都准备好了,再统一导出,就像某些厨师,菜洗好、姜、葱、蒜等都弄好了,放那,才开始开火炒菜; 有些人呢,喜欢编写完一个导出一个,可能油都冒烟儿了才准备摘菜 ~ 可能有点晚了哈 ~ -_-#。这两种,在我看来,全凭个人好恶, 没有实质区别,不过我们在使用方式二/三的时候需要注意这种情况:

/*module.exports = {
    fun: function(name) {
        console.log('hello, ',name) ;
    }
} ;*/

/*function fn(name){
    console.log('hello, ',name) ;
} ;
exports.fun = fn ;*/

exports = {
    fun : function(name) {
        console.log('hello, ',name) ;
    }
} ;

这时候使用node 命令运行,shell立马告诉我们出错了:

[palm@arch]: ~/Desktop/node-stu/stunode>$ node nd-test.js 
/home/palm/Desktop/node-stu/stunode/nd-test.js:2
nd.fun('palm') ;
   ^

TypeError: nd.fun is not a function
    at Object.<anonymous> (/home/palm/Desktop/node-stu/stunode/nd-test.js:2:4)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

告诉我们有TypeError(javascript Error子类),nd.fun is not a function 没有找到函数fun 或者fun 不是一个函数 ,为什么呢?
把require到的对象打印出来:

var nd = require('./nd') ;
// nd.fun('palm') ;

console.log(nd) ;  //print : {}

是一个空对象,在nd.js文件中,把exports对象打印出来:


console.log(' before ',exports) ;
exports = {
    fun : function(name) {
        console.log('hello, ',name) ;
    }
} ;

console.log(' after ',exports) ;

//print
 before  {}
 after  { fun: [Function: fun] }

嗯? 函数fun 在exports中啊,为毛到另一个文件中exports对象就变成空对象了?原来exports虽然也是一个全局变量,但它只是模块内为了方便通过exports访问module.exports 的一个module-global, exports是指向module.exports的一个引用,类似一个指针,如果我们将一个新的对象重新指向exports 则,exportsmodule.exports就不再绑定到一起了,我想在Node.js module system创建 module.exports对象的时候代码代码大概如下:

exports = module.exports = {} ;

module.exports 是Node.js模块系统创建的,导入模块传递的也是该变量,而不是exports。
require 导入模块后从module.exports 取出内容后值为空,解释在这里 require函数类似如下片段:

function require(...) {
  // ...
  ((module, exports) => {
    // Your module code here
    exports = some_func;        // re-assigns exports, exports is no longer
                                // a shortcut, and nothing is exported.
    module.exports = some_func; // makes your module export 0
  })(module, module.exports);
  return module;
}

所以,在编写自定义模块的时候极力建议使用第一种方式,或者:

function fun0(name) {
    console.log('hello, ',name) ;
}

function fun1(arg) {
    console.log('arg') ;
}

module.exports.fn = fun ;
module.exports.fn1 = fun1 ;

模块内变量
在Node.js中,模块内var a = 'xxx' 声明变量,都是私有的,如果没有使用exports导出外部均不能使用,Node.js使用了一个官方称为module wrapper 的东西进行处理,通俗的说就是将我们编写的代码使用function 包了一下,为什么使用function包一层就可能达到模块本地变量私有化的目的呢? 这是因为在javascript中,没有像java 或者python 类似的块级作用域,在javascript中,只有函数级作用域,函数内部生命变量外部不可以访问,如:

function fn() {
    var fnx1 = 1234 ;
}

console.log(' fn.fnx1 ', fnx1) ;

node.js shell 提示如下:

console.log(' fn.fnx1 ', fnx1) ;
                         ^

ReferenceError: fnx1 is not defined
    at Object.<anonymous> (/home/palm/Desktop/node-stu/stunode/nd.js:44:26)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

chrome中,console错误提示:

js.js:46 Uncaught ReferenceError: fnx1 is not defined

在模块代码执行之前,Node.js会将自定义模块代码包装成如下这样:

(function (exports, require, module, __filename, __dirname) {
// Your module code actually lives in here
});

//__filename和__dirname 是模块内文件属性的内置变量 
//__filename包含了模块文件的绝对路径 ; 
//__dirname 包含模块文件的目录path.

如此,模块内本地变量都是私有的,在工作中,也时常见到猿类利用js这个特性来将某些变量隐藏,或者给老板节省一点儿内存开销, 或者相同变量冲突. 如:

var arr = [] ;

for(var i = 0; i < 50; i++ ){
    var node = {}, filename = '/home/palm/Desktop/node-stu/stunode/nd.js' ;
    node['__filename'] = filename;
    node['xx'] = 'test' ;

    arr.push(node) ;
}


for(var i = 0; i < 50; i++ ){
    var node = {}, filename = '/home/palm/Desktop/node-stu/stunode/nd-test.js' ;
    node['__filename'] = filename;
    node['xx'] = 'test' ;

    arr.push(node) ;
}

以上代码,四个变量nodex2 、 filenamex2 其实都是全局变量, 如:

var arr = [] ;

for(var i = 0; i < 50; i++ ){
    var node = {}, filename = '/home/palm/Desktop/node-stu/stunode/nd.js' ;
    node['__filename'] = filename;
    node['xx'] = 'test' ;

    arr.push(node) ;
}


for(var i = 0; i < 50; i++ ){
    var node = {}, filename = '/home/palm/Desktop/node-stu/stunode/nd-test.js' ;
    node['__filename'] = filename;
    node['xx'] = 'test' ;

    arr.push(node) ;
}

console.log(node,filename) ;

//print:

{ 
__filename: '/home/palm/Desktop/node-stu/stunode/nd-test.js',
  xx: 'test' 
} 
'/home/palm/Desktop/node-stu/stunode/nd-test.js'

为了避免自己的的私有变量被污染或者变量常驻内存而导致内存泄漏,可以采用如下方式改变这一状况,如:

var arr = [] ;

(function() {
    for(var i = 0; i < 50; i++ ){
        var node = {}, filename = '/home/palm/Desktop/node-stu/stunode/nd.js' ;
        node['__filename'] = filename;
        node['xx'] = 'test' ;

        arr.push(node) ;
    }
})() ;


(function() {
    for(var i = 0; i < 50; i++ ){
        var node = {}, filename = '/home/palm/Desktop/node-stu/stunode/nd-test.js' ;
        node['__filename'] = filename;
        node['xx'] = 'test' ;

        arr.push(node) ;
    }
})() ;

console.log(node,filename) ;

执行上面代码,一定会输出 ReferenceError ,因为确实找不到 这两个变量定义了。如:

console.log(node,filename) ;
            ^

ReferenceError: node is not defined
    at Object.<anonymous> (/home/palm/Desktop/node-stu/stunode/nd.js:63:13)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

这里能arr 变量能访问是因为arr全局变量,js中,作用域内函数可以访问作用域内任何变量(作用域内其他函数内部变量除外)。

这里有更多关于module对象属性的功能描述和用法。

js模块化编程已经是一个趋势了,各种js框架都在积极推进这一工作,例如:

  • RequireJS 在推广过程中对模块化的规范定义 -- AMD
  • SeaJS 在推广过程中对模块化的规范定义 -- CMD
  • BravoJS 的 CommonJS Modules/2.0 规范等

目的是为了解决前端js代码混乱难以维护、代码复用率低等问题.尤其是在浏览器端。

module的学习到这里就结束了,如果看到有错误的地方,请帮我指出来,我将感激不尽!

!~

相关文章

  • webpack+vue初始化项目

    第1步:官网下载并安装nodejs nodejs官网:http://nodejs.cn/download/ 第2步...

  • VUE+webpack初始化项目

    第1步:官网下载并安装nodejs nodejs官网:http://nodejs.cn/download/ 第2步...

  • Mac 下 nodejs 学习

    Mac 下 nodejs 学习 Mac 下 nodejs 学习http://www.jianshu.com/p/a...

  • Vert.x框架 --- JVM上node.js的替代者

    学习nodejs

  • Nodejs学习第9天

    在Node.js中,有一个简单的模块加载系统,在Node.js的模块中,可以直接和文件通信,不仅仅是*.js文件。...

  • Nodejs学习第5天

    Node.js内置基础模块fs, Node.js 提供了基础的文件操作,并同时提供文件的同步和异步操作,如: .....

  • Nodejs学习第6天

    fs.readFileSync(file[, options]) 同步读取文件内容到内存中,如: fs.readF...

  • Nodejs学习第8天

    Events - events模块是Node.js的核心模块、核心API之一,它是Node.js事件驱动模型的核...

  • Nodejs学习第4天

    之前我学习了使用Node.js实现一个简单的Server (见Nodejs学习第一天)这让我想起了当初学习java...

  • 2018-08-21nodejs

    Nodejs学习笔记 一、 NodeJs介绍 什么是NodeJS,在应用程开发中起什么作用? Nodejs是一个应...

网友评论

    本文标题:Nodejs学习第9天

    本文链接:https://www.haomeiwen.com/subject/sshhuttx.html