Read all text from stdin to a string

前端 未结 4 1301
孤城傲影
孤城傲影 2020-12-16 09:26

I\'m writing a program in Node.js that (in some situations) wants to act as a simple filter: read everything from stdin (up to end of file), do some processing, write the re

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-16 09:48

    I use the following in Node 11+

     async function read(stream) {
       const chunks = [];
       for await (const chunk of stream) chunks.push(chunk); 
       return Buffer.concat(chunks).toString('utf8');
     }
    

    Usage:

    const input = await read(process.stdin);
    

提交回复
热议问题