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.

你可能感兴趣的:(Node.js学习笔记2——核心模块)