Node.js学习笔记2——核心模块

Changing input method when editing markdown blogs is so complex and waste of time.
So from now, I will write blogs in English. Of course, if the blog is too difficult to express in English, I will switch back to Chinese. My English is bad, but I want to try. thanks

process

process is a global variant, it's a property of global object.
It is used to describe current process' status of Node.js, and it provide a sample interface to operation system.

  • process.argv
    it is a command line parameters array. first element is node, second element is script file name, from the third element, every element is a input parameter.

we can print it:

console.log(process.argv);
  • process.stdout
    it is the standard output stream, we usually use console.log() to print characters to standard output, and process.stdout.write() function provide a lower interface.

  • process.stdin
    it is the standard input stream, at init time, it is paused, when you want to read data from stdin, we must to resume the stream, and write the event respond function by hand.

process.stdin.resume();
process.stdin.on('data', function(data) {   
    process.stdout.write('read from console: ' + data.toString());
});
  • process.nextTick(callback)
    it is used to assign a task for event loop, Node.js will invoke the callback method on next event loop.
    nextTick provide a tool, to separate complex task into smaller ones.

console

console is used to provide a standard output of console.

console.log('Hello world');
console.log('realank%');
console.log('realank%', 1990);

//-result-
//Hello world
//realank%
//realank1990
  • console.error(): same usage with console.log(), but is output to standard error stream.

  • console.trace(): output current invoke stack to standard error stream.

events (event driver)

events is the most important module in Node.js, no 'one of', it's the foundation of Node.js event coding.

event emitter

events module only provide one object: events.EventEmitter.


var events = require('events');
var emitter = new events.EventEmitter();
emitter.on('someEvent', function(arg1, arg2) { 
    console.log('listener1', arg1, arg2);
});
emitter.on('someEvent', function(arg1, arg2) { 
    console.log('listener2', arg1, arg2);
});
emitter.emit('someEvent', 'realank', 1990); 
//results:
//    listener1 realank 1990
//    listener2 realank 1990
common API of EventEmitter
  • EventEmitter.on(event, listener):
    event is a string to identify a specific event, listener is a callback function, when event occurs, listener will be invoked.
  • EventEmitter.emit(event, [arg1], [arg2], [...]):
    you can pass many parameters(optional) to listeners
  • EventEmitter.once(event, listener):
    register a 'one-time' listener for a specific event.
  • EventEmitter.removeListener(event, listener):
    remove a listener from the event, this listener must be registered
  • EventEmitter.removeAllListeners([event]):
    remove all listeners of all events. if event is given, remove all listeners of only this event.

error event

EventEmitter defines a special event error, it include a 'error' semantics.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,482评论 0 23
  • 把事做好,把人做好事做好是前提
    咸叔说阅读 937评论 0 0
  • 安全,这个世界每个人都在寻找的地方,找的很辛苦! 当大家都累了时候,却把这种希望寄托在别人的身上,特别是生活在北上...
    时间的朋友2016阅读 876评论 0 1
  • 昨天和两个朋友在一起聊到了孩子的问题,他们都感觉我还是太紧张了,叫我放松,尽量少说。特别是在去年的时候,我跟朋友聊...
    xyldaiqun阅读 1,040评论 3 4