Node.js readStream.setRawMode() 方法
readStream.setRawMode()方法是 'tty' 模块中 ReadStream 类的内置应用程序编程接口,用于将读取流对象的原始模式设置为作为原始设备工作。
句法:
const readStream.setRawMode( mode )
参数:此方法将布尔值作为参数。
返回值:此方法不返回任何值。
示例 1:文件名:index.js
Javascript
// Node.js program to demonstrate the
// readStream.setRawMode() method
// Importing dgram module
var dgram = require('dgram');
// Creating and initializing client
// and server socket
var client = dgram.createSocket("udp4");
var server = dgram.createSocket("udp4");
// Catching the message event
server.on("message", function (msg) {
// Creating and initializing a
// ReadStream object
let ReadStream = process.stdin;
// Setting the read stream Raw mode
// by using setRawMode() method
ReadStream.setRawMode(false);
// Checking if it is configured or not
// by using isRaw() method
const status = ReadStream.isRaw;
// Displaying the result
if (status)
process.stdout.write(msg + "configured" + "\n");
else
process.stdout.write(msg + "not configured" + "\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.setRawMode() method
// Creating and initializing a ReadStream object
let ReadStream = process.stdin;
// Setting the read stream Raw mode
// by using setRawMode() method
ReadStream.setRawMode(true);
// Checking if it is configured or not
// by using isRaw() method
const status = ReadStream.isRaw;
// Displaying the result
if(status)
console.log("It is configured");
else
console.log("It is not configured");
输出:
It is not configured
示例 2:文件名:index.js
Javascript
// Node.js program to demonstrate the
// readStream.setRawMode() method
// Creating and initializing a ReadStream object
let ReadStream = process.stdin;
// Setting the read stream Raw mode
// by using setRawMode() method
ReadStream.setRawMode(true);
// Checking if it is configured or not
// by using isRaw() method
const status = ReadStream.isRaw;
// Displaying the result
if(status)
console.log("It is configured");
else
console.log("It is not configured");
输出:
It is configured
使用以下命令运行 index.js 文件:
node index.js
参考: https://nodejs.org/dist/latest-v12.x/docs/api/tty.html#tty_readstream_setrawmode_mode