"process.stdin.on" 中的“.on”是做什么用的?
What is the ".on" in "process.stdin.on" for?
我在 Node.js 网站上,我从朋友那里得到了一些包含 "stdin" 的示例代码。
我去搜索标准输入是什么,我现在知道了。
虽然,在 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 documentation:
The process
object is an instance of EventEmitter
如果您转到 EventEmitter documentation,您可以在那里找到有关 API 和 on
功能的更多信息:
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.
在我的例子中,它通过以下 API 方法查看了 Node 的 TypeScript 定义文件,使我走上了这条路:
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)[];
}
.on()
用于监听一个事件。这就像在 HTML 按钮上监听 click
事件。实际上 jQuery 在 DOM 事件中有完全相同的用法。
The process.stdin
property returns a net.Socket
Stream connected to stdin. ^
It extends from stream.Duplex
which makes it both readable and writable. ^
All streams are instances of EventEmitter. ^
现在我们了解了这一点,让我们看看您可以在 process.stdin
中找到什么。
.addListener(eventName, listener)
^
.on(eventName, listener)
的别名
.on(eventName, listener)
^
- 将
listener
函数添加到名为 eventName
的事件的侦听器数组的末尾
.once(eventName, listener)
^
- 为名为
eventName
的事件添加一次性 listener
函数。下次 eventName
被触发时,这个 listener
被移除然后被调用。
.off(eventName, listener)
^
- 从名为
eventName
的事件的侦听器数组中删除指定的 listener
.removeListener(eventName, listener)
^
.off(eventName, listener)
的别名
这意味着您可以附加 "listeners" 执行操作以响应标准输入发出的各种事件。
例如,当用户按下一个键时。它类似于 DOM 事件,例如 onClick="myFunction()"
属性,object.onclick
或 jQuery.on('click')
。
不确定这个问题是否需要另一个答案,但非常简单 process.stdin.on();
(如上所述)只是一个侦听器 - 它正在侦听 'data' 事件,当用户点击 'enter' 在键盘上。当 .on()
听到事件时,您可以通过提供回调来做某事。在幕后,Node 会将用户输入传递给您的回调。
这是一个非常简单的例子:
// Listen for the 'data' event, then call myCallback...
process.stdin.on('data', myCallback);
let myCallback = (userInput) => {
// In Node the userInput is a Buffer class instance, so .toString() it
// It also comes with a new line character, so let's trim it off
let input = userInput.toString().trim();
// Now do whatever you want with the userInput...
};
通常你也会编写一个发射器来触发你正在监听的事件,比如:
myEmitter.emit('myEvent', 'The event happened!');
但您不需要 - 在这种情况下,只要您在键盘上按下 'enter',Node 就会为您发出事件。您所要做的就是用 .on()
收听它,然后做一些回应。
我在 Node.js 网站上,我从朋友那里得到了一些包含 "stdin" 的示例代码。 我去搜索标准输入是什么,我现在知道了。 虽然,在 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 documentation:
The
process
object is an instance ofEventEmitter
如果您转到 EventEmitter documentation,您可以在那里找到有关 API 和 on
功能的更多信息:
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.
在我的例子中,它通过以下 API 方法查看了 Node 的 TypeScript 定义文件,使我走上了这条路:
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)[];
}
.on()
用于监听一个事件。这就像在 HTML 按钮上监听 click
事件。实际上 jQuery 在 DOM 事件中有完全相同的用法。
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. ^
现在我们了解了这一点,让我们看看您可以在 process.stdin
中找到什么。
.addListener(eventName, listener)
^
.on(eventName, listener)
的别名
.on(eventName, listener)
^
- 将
listener
函数添加到名为eventName
的事件的侦听器数组的末尾
.once(eventName, listener)
^
- 为名为
eventName
的事件添加一次性listener
函数。下次eventName
被触发时,这个listener
被移除然后被调用。
.off(eventName, listener)
^
- 从名为
eventName
的事件的侦听器数组中删除指定的
listener
.removeListener(eventName, listener)
^
.off(eventName, listener)
的别名
这意味着您可以附加 "listeners" 执行操作以响应标准输入发出的各种事件。
例如,当用户按下一个键时。它类似于 DOM 事件,例如 onClick="myFunction()"
属性,object.onclick
或 jQuery.on('click')
。
不确定这个问题是否需要另一个答案,但非常简单 process.stdin.on();
(如上所述)只是一个侦听器 - 它正在侦听 'data' 事件,当用户点击 'enter' 在键盘上。当 .on()
听到事件时,您可以通过提供回调来做某事。在幕后,Node 会将用户输入传递给您的回调。
这是一个非常简单的例子:
// Listen for the 'data' event, then call myCallback...
process.stdin.on('data', myCallback);
let myCallback = (userInput) => {
// In Node the userInput is a Buffer class instance, so .toString() it
// It also comes with a new line character, so let's trim it off
let input = userInput.toString().trim();
// Now do whatever you want with the userInput...
};
通常你也会编写一个发射器来触发你正在监听的事件,比如:
myEmitter.emit('myEvent', 'The event happened!');
但您不需要 - 在这种情况下,只要您在键盘上按下 'enter',Node 就会为您发出事件。您所要做的就是用 .on()
收听它,然后做一些回应。