“process.stdin.on”中的“.on”在哪里?

我是Node.js的新手

我在他们的网站上,我有一个朋友的示例代码包含“标准input”。 我去寻找什么标准input,我现在知道。 另外,在Node.js的网站上,他们使用“ stdin.on ”。

我找不到任何关于它的事情。 也许有人可以填补我? 🙂

process.stdin.setEncoding('utf8'); process.stdin.on('readable', () => { var chunk = process.stdin.read(); if (chunk !== null) { process.stdout.write(`data: ${chunk}`); } }); process.stdin.on('end', () => { process.stdout.write('end'); }); 

我希望有人能够在非专家层面上向我解释这一点。 我知道我可以买一本书,然后开始阅读,但是我在业余时间为了娱乐而做这个。

最近自己也在同一个问题上挣扎,经过一番挖掘,我发现根据Node.Js文档 :

process对象是EventEmitter一个实例

如果你转到EventEmitter文档,你可以find更多关于API和on那里的function:

将侦听器函数添加到名为eventName的事件的侦听器数组的末尾。 没有检查是否已经添加了侦听器。 传递相同的eventName和listener组合的多个调用将导致多次添加和调用侦听器。

在我的情况下,它正在通过TypeScript定义文件中的Node来查看该路由,并使用以下API方法:

 export class EventEmitter { addListener(event: string | symbol, listener: Function): this; // Here is it on(event: string | symbol, listener: Function): this; once(event: string | symbol, listener: Function): this; removeListener(event: string | symbol, listener: Function): this; removeAllListeners(event?: string | symbol): this; setMaxListeners(n: number): this; getMaxListeners(): number; listeners(event: string | symbol): Function[]; emit(event: string | symbol, ...args: any[]): boolean; listenerCount(type: string | symbol): number; // Added in Node 6... prependListener(event: string | symbol, listener: Function): this; prependOnceListener(event: string | symbol, listener: Function): this; eventNames(): (string | symbol)[]; }