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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。