zjffun blog

Node Pipe

更新于 写于 Node

All data:

js
async function main() {
  let data = "";
  for await (const chunk of process.stdin) data += chunk;

  // process all the data and write it back to stdout
  process.stdout.write(data);
}

main();

Line by line:

js
const readline = require("node:readline");

async function main() {
  const rl = readline.createInterface({
    input: process.stdin,
  });

  for await (const line of rl) {
    // process a line at a time
    process.stdout.write(`line: ${line}\n`);
  }
}

main();

See: