當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。