I was on the Node.js website and I had some example code from a friend containing "stdin". I went searching what stdin was and I do know now. Although, on the website from Node.js, they use "stdin.on".
I cannot find ANYTHING about it. Maybe someone can fill me in?! :)
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 someone could explain this to me on a non-expert level.
stdin property is an inbuilt application programming interface of the process module which listens for the user input. The stdin property of the process object is a Readable Stream. It uses on() function to listen for the event.
`process. stdin. pause` will "close" `stdin`.
stdin (0): The standard input stream, which is a source of input for the program. process. stdout (1): The standard output stream, which is a source of output from the program. process. stderr (2): The standard error stream, which is used for error messages and diagnostics issued by the program.
The
process.stdin
property returns anet.Socket
Stream connected to stdin. ^It extends from
stream.Duplex
which makes it both readable and writable. ^All streams are instances of EventEmitter. ^
Now that we understand this, let's look what really is that you can find in process.stdin
.
.addListener(eventName, listener)
^
.on(eventName, listener)
.on(eventName, listener)
^
listener
function to the end of the listeners array for the event named eventName
.once(eventName, listener)
^
listener
function for the event named eventName
. The next time eventName
is triggered, this listener
is removed and then invoked..off(eventName, listener)
^
listener
from the listener array for the event named eventName
.removeListener(eventName, listener)
^
.off(eventName, listener)
What this means is that you can attach "listeners" which execute actions in response to various events emitted by stdin.
For example, when user presses a key. It is similar to DOM events such as onClick="myFunction()"
attribute, object.onclick
or jQuery.on('click')
.
Been struggling with the same question myself recently and after a bit of digging I found that according to the Node.Js documentation:
The
process
object is an instance ofEventEmitter
If you head over to the EventEmitter documentation you can find more about the API and the on
functionality there:
Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.
In my case it was looking through the TypeScript definition file for Node that led me down that 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)[];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With