当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js readStream.isTTY用法及代码示例


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

用法:

const readStream.isTTY

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

范例1: 文件名:index.js

// 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 intializing 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");

输出:



It is an instant of TTY

范例2: 文件名:index.js

// 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




相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Node.js readStream.isTTY Property。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。