Where is the ".on" for "process.stdin.on"?

I am new to Node.js.

I was on their website and I had a sample code from a friend containing "stdin". I went looking for what was stdin, and now I know. Although on the website from Node.js they use " stdin.on ".

I can’t find anything. Maybe someone can fill me ?! :)

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'); }); 

I was hoping that someone could explain this to me at a non-expert level. I know that I can buy a book and start reading, but I do it for pleasure in my free time ...

+5
source share
1 answer

I recently struggled with the same issue, and after a little digging, I found that according to Node.Js the documentation :

The process object is an instance of EventEmitter

If you go to the EventEmitter documentation , you can find more about the API functions and on :

Adds a listener function to the end of the listener array for an event named eventName. It does not check if a listener has been added. Multiple calls passing the same combination of eventName and listener will cause the listener to be added and called several times.

In my case, he was looking at the TypeScript definition file for Node, which led me along this route with the following API methods:

 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)[]; } 
+3
source

Source: https://habr.com/ru/post/1262813/


All Articles