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


Node.js writeStream.isTTY用法及代碼示例


writeStream.isTTY屬性是‘tty’模塊內類WriteStream的內置應用程序編程接口,用於檢查write Stream對象是否為tty實例。對於tty,writeStream,它將始終返回true。

用法:

const writeStream.isTTY

返回值:如果write Stream對象是tty的實例,則此屬性返回true。

範例1: 文件名:index.js

Javascript



// Node.js program to demonstrate the 
// writeStream.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"); 
  
// Cathing the message event 
server.on("message", function (msg) { 
  
    // Creating and initializing a  
    // WriteStream object 
    let WriteStream = process.stdout; 
  
    // Checking if it is instance of TTY 
    // or not by using isTTY() method 
    const status = WriteStream.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

Javascript

// Node.js program to demonstrate the 
// writeStream.isTTY() property 
  
// Creating and initializing a  
// WriteStream object 
let WriteStream = process.stdout; 
  
// Checking if it is instance of  
// TTY or not by using isTTY() method 
const status = WriteStream.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

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

node index.js

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

相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Node.js writeStream.isTTY Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。