📜  Node.js readStream.isTTY 属性

📅  最后修改于: 2022-05-13 01:56:28.448000             🧑  作者: Mango

Node.js readStream.isTTY 属性

readStream.isTTY属性是 tty 模块中 ReadStream 类的内置应用程序编程接口,用于检查读取的流对象是否为 tty 的实例。对于 tty、ReadStream,它将始终返回 true。

句法:

const readStream.isTTY

返回值:如果读取的流对象是 tty 的实例,则此属性返回 true。

示例 1:文件名:index.js

javascript
// Node.js program to demonstrate the
// readStream.isTTY property
 
// Importing dgram module
var dgram = require('dgram');
 
// Creating and initializing client
// and server socket
var client = dgram.createSocket("udp4");
var server = dgram.createSocket("udp4");
 
// Handling the message event
server.on("message", function (msg) {
 
    // Creating and initializing a
    // ReadStream object
    let ReadStream = process.stdin;
 
    // Checking if it is instance of TTY
    // or not by using isTTY() method
    const status = ReadStream.isTTY;
 
    // Displaying the result
    if (status) {
        process.stdout.write(msg
            + "an instant of TTY" + "\n");
    } else {
        process.stdout.write(msg
            + "not an instant of TTY" + "\n");
    }
 
    // Exiting process
    process.exit();
})
 
// Binding server with port
.bind(1234, () => {
});
 
// Client sending message to server
client.send("It is ", 0, 7, 1234, "localhost");


javascript
// Node.js program to demonstrate the
// readStream.isTTY property
 
// Creating and initializing a
// ReadStream object
let ReadStream = process.stdin;
 
// Checking if it is instance of TTY
// or not by using isTTY() method
const status = ReadStream.isTTY;
 
// Displaying the result
if(status) {
   console.log("It is an instant of TTY");
} else {
   console.log("it is not an instant of TTY");
}


输出:

It is an instant of TTY

示例 2:文件名:index.js

javascript

// Node.js program to demonstrate the
// readStream.isTTY property
 
// Creating and initializing a
// ReadStream object
let ReadStream = process.stdin;
 
// Checking if it is instance of TTY
// or not by using isTTY() method
const status = ReadStream.isTTY;
 
// Displaying the result
if(status) {
   console.log("It is an instant of TTY");
} else {
   console.log("it is not an instant of TTY");
}

使用以下命令运行 index.js 文件:

node index.js

输出:

It is an instant of TTY

参考: https://nodejs.org/dist/latest-v12.x/docs/api/tty.html#tty_readstream_istty