1 核心模块主要内容:
2 全局对象
3 常用工具
4 事件机制
5 文件系统访问
6 http服务器和客户端
7
8 global object:
9 所有的全局变量(除了global本身外)都是global object 的属性(attribute)
10
11 global object,global variable
12
13 global的根本作用就最为全局变量的宿主:
14 what is global variable?:
15 在最外层定义的
16 全局对象的属性
17 隐式定义的变量(未定义直接赋值的变量)
18
19 Notice: 在Node.js中,你不可能在最外层定义变量,因为所有的用户代码都是当前模块的,二模块本身不是最外层上下文
20
21 var 声明的不是全局变量
22
23 process ->global variable //Desc the current of node.js process status
24 process.argv是命令行参数数组,第一个元素是node,第二个是脚本文件名,第三个是参数
25
26 //for example
27 /*
28 *author:e路相扶
29 *filename argv.js
30 */
31 console.log(process.argv);
32 $ node argv.js 1990 name=eself --v 'zhangjun'
33
34 output:
35 ['node','/home/argv.js','name=eself','--v','zhangjun'];
36
37 process.stdout是标准的输出流,
38 process.stdout.write()函数提供了更底层的接口
39 process.stdin是标准的输入流,初始时,她是被暂停的,要想从标准输入读取数据,必须恢复流,并且手动编写流的事件响应函数
40
41 /*
42 *author:e路相扶
43 *filename stdin.js
44 */
45 process.stdin.resume();
46 process.stdin.on('data',function(data)){
47 process.stdout.write('read from console :'+ data.toStrin());
48 }
49
50 process.nextTick(callback)的功能是为事件循环设置一项任务,node.js会在事件循环响应时调用callback
51 node.js的一个编程原则是尽量缩短每个事件的执行时间,process.nickTick()提供了这样的工具
1 /*
2 *author:e路相扶
3 *filename nextTick.js
4 */
5
6
7 function doSomething(args,callback){
8 somethingComplicated(args);
9 process.nextTick(callback);
10 }
11 doSomething(function onEnd(){
12 compute();
13 });
14 -----------------------------------------
15 Notice :不要使用setTimeout(fn,0)代替process.nextTick(callback),前者比后者的效率要低很多
16
17 process还有很多成员,详细地址查看 http://nodejs.org/api/process.html
18
19 console :
20 console用于提供控制台标准的输出,
21 console.log(variable),如果一个参数,则输出这个参数的字符串形式,多个参数,则参照c的printf()命令的格式
22 //js中也有这个,比alert()测试好用--个人感觉
23 console.error() :the same as console.log() 只是像标准错误流输出
24 console.trace()向标准错误流输出当前的调用栈
25
26 Util://提供常用函数的集合
27 util.inherits(constructor,superConstructor)是一个实现对象间原型继承的函数
28 /*
29 *author:e路相扶
30 *filename util.js
31 */
32
33 var util=require('util');
34 function Base(){
35 this.name='base';
36 this.base='1990';
37 this.sayHello=function(){
38 console.log('Hello '+this.name);
39 }
40 }
41 Base.prototype.showName=function(){
42 console.log(this.name);
43 }
44 function sub(){
45 this.name='sub';
46 }
47 util.inherits(sub,Base);
48 var objBase=new Base();
49 objBase.showName();// base
50 objBase.sayHello();//hello base
51 console.log(objBase);//{name:'base',base:1991,sayHello:[function]} sub {name:'sub'}
1 util.inspect(object,[showHidden],[depth],[colors]) 是一个将任意对象转换为字符串的方法,通常用于调试和错误输出
2 object:将要转换的对象
3 showHidden:如果是true,将会输出更多隐藏信息
4 depth:表示最大递归层数,不指定默认为递归2层,指定为null表示不限递归层数,完整便利对象
5 color:true输出格式将会以ANSI颜色编码
6 util.inspect并不会简单地直接把对象转换为字符串,即使该对象定义了toString方法也不会调用。
7 /*
8 *author:e路相扶
9 *filename inspect.js
10 */
11 var util=require('util');
12 function Person(){
13 this.name='e路相扶';
14 this.toString=function(){
15 return this.name;
16 }
17 }
18 var obj=new Person();
19 console.log(util.inspect(obj));
20 console.log(util.inspect(obj,true));
21
22 output:
23 {name:'e路相扶',toString:[function]}
24 {toString:
25 {[function]
26 [prototype]:{[constructor]:[Circular]},
27 [caller]:null,
28 [length]:0,
29 [name]:'',
30 [arguments]:null},
31 name:'e路相扶'
32 }
33
34 除了上面几个函数,util.isArray(),util.isRegExp(),util.isDate(),util.isError(),util.format(),
35 util.debug()等我们可以在http://nodejs.org/api/util.html 了解详细内容
36
37 event-driven events:
38 events 是Node.js最重要的模块,没有之一,Node.js本身架构就是事件式的
39 事件发射器:
40 events模块只能提供一个对象,events.EventEmitter
41 events.EventEmitter核心就是事件发射与事件监听功能的封装,EventEmitter的每个事件就是由一个事件名和
42 若干个参数组成,事件名是一个字符串,通常表达一定的语义。对于每个事件,EventEmitter支持若干个事件监听器
43 当事件发射时,注册到这个事件的事件监听器被一次调用,事件参数为回调函数参数传递
44 /*
45 *author:e路相扶
46 *filename events.js
47 */
48 var events=require('events');
49 var emitter=new events.EventEmitter();
50 emitter.on('someEvent',function(arg1,arg2){
51 console.log('listener1',arg1,arg2);
52 });
53 emitter.on('someEvent',function(arg1,arg2){
54 console.log('listener2',arg1,arg2);
55 });
56 emitter.emit('someEvent','e路相扶',1990);
57
58 output:
59 listener1 e路相扶 1990
60 listener2 e路相扶 1990