我爱编程

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

2017-03-14  本文已影响44人  Realank

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.

we can print it:

console.log(process.argv);
process.stdin.resume();
process.stdin.on('data', function(data) {   
    process.stdout.write('read from console: ' + data.toString());
});

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

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

error event

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

上一篇 下一篇

猜你喜欢

热点阅读