javascript - 為什么 process.stdin 會(huì)有 write 方法? ???
問題描述
寫在前面根據(jù) 官方API,在 stream 中只有可寫流才會(huì)有 write 方法,但是有這樣一個(gè)例子卻使用 process.stdin.write() 發(fā)送數(shù)據(jù)。而 process.stdin 在 官方API 中是可讀流。
問題:RT,如果是其他的原因造成這樣一個(gè)情況,麻煩詳細(xì)描述一下。謝謝
參考代碼:stdin.js
/** * 監(jiān)聽輸入的數(shù)據(jù),并輸出到控制臺(tái) */// 重新開始 stdin streamprocess.stdin.resume();// 監(jiān)聽輸入的數(shù)據(jù)process.stdin.on(’data’, function (data) { var number; try { // 將輸入的信息解析成數(shù)字 number = parseInt(data.toString(), 10); // 自增1 number += 1; // 輸出 數(shù)字 process.stdout.write(number + 'n',function() { console.log(1); }); } catch (err) { process.stderr.write(err.message + 'n'); }});
stdin_test.js
var spawn = require(’child_process’).spawn;// 使用 node 進(jìn)程創(chuàng)建一個(gè)子進(jìn)程執(zhí)行 stdin.jsvar child = spawn(’node’, [’stdin.js’]);// 每隔一秒調(diào)用一次該函數(shù)setInterval(function() { // 生成一個(gè)小于 10 的隨機(jī)數(shù) var number = Math.floor(Math.random() * 10); // 將該隨機(jī)數(shù)發(fā)送到子進(jìn)程 child.stdin.write(number + 'n'); // 監(jiān)聽子進(jìn)程的輸出 并打印出來 // 此處對(duì)應(yīng) stdin.js 的 process.stdout.write child.stdout.once(’data’, function(data) { console.log(’child replied to ’ + number + ’ with: ’ + data); });}, 1000);child.stderr.on(’data’, function(data) { process.stdout.write(data);});
問題解答
回答1:注意這里的 stdin 是 ChildProcess 的,是可寫流
https://nodejs.org/dist/lates...
相關(guān)文章:
1. boot2docker無法啟動(dòng)2. node.js - antdesign怎么集合react-redux對(duì)input控件進(jìn)行初始化賦值3. 關(guān)于docker下的nginx壓力測(cè)試4. pdo_mysql 值自增寫法5. node.js mysql Cannot find module "net" 和 "tls"和"fs" 的問題6. mysql - InnoDB中的鎖7. 遠(yuǎn)程連接阿里云上的Mysql,連接不上!8. javascript - 在低版本微信如何處理引用高版本的微信小程序基礎(chǔ)庫所帶來的問題?9. mysql - sql數(shù)據(jù)還原10. MySQL客戶端吃掉了SQL注解?

網(wǎng)公網(wǎng)安備