How to take in text input from a keyboard and store it into a variable?

前端 未结 7 2050
清酒与你
清酒与你 2020-12-30 05:43

I would just like something simple to read text from a keyboard and store it into a variable. So for:

var color = \'blue\'

I would like the

相关标签:
7条回答
  • 2020-12-30 06:19

    We can also use NodeJS core standard input functionality for the same. ctrl+D for end standard input data reading.

    process.stdin.resume();
    process.stdin.setEncoding("utf-8");
    var input_data = "";
    
    process.stdin.on("data", function(input) {
      input_data += input; // Reading input from STDIN
      if (input === "exit\n") {
        process.exit();
      }
    });
    
    process.stdin.on("end", function() {
      main(input_data);
    });
    
    function main(input) {
      process.stdout.write(input);
    }
    
    0 讨论(0)
提交回复
热议问题