javascript node.js读取命令行流
我有一个node.js脚本,该脚本使用npm命令程序读取args。
例如node a.js -a "hello" -b "100"
但是在将html数据发送给这样的时候,我有一个问题
node a.js -a "some Html content with double quotes and single quotes" -b "100"
我认为可以发送EOL之类的选项
node a.js -b "100" << EOL
some Html content with double quotes and single quotes
EOL
我如何在node.js中阅读此信息(使用命令程序:)
回答如下:您可以通过这种方式使用readline
内置的node.js模块:readline
用const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
});
let html = ''
rl.on('line', (input) => {
if (input === 'EOL')
console.log('received html: ' + html)
else
html+= ' ' + input
});
调用,然后输入HTML,并使用所需的任意行。当输入EOL(在一行中)时,程序将认为HTML输入已完全提供。