How do I call the stdin.on ("data", [callback]) event in code?

Consider the following example

process.stdin.resume();
process.stdin.on("data", function(data) {
  console.log("recieved " + data)
})

process.stdin.write("foo\n")
process.stdin.write("bar\n")

      

When I type something

in the terminal, I get

 received something

      

Why doesn't it work the same for foo

and bar

that I posted earlier using stdin.write

?

eg. how can i call this event ( stdin.on("data)

) in code? I expected to process.stdin.write

do this, but I only get the same result.

+3


source to share


1 answer


It is a readable stream that receives its input from a file descriptor stdin

. I don't think you can write to this descriptor (but you can connect it to another writeable descriptor).

However, the simplest solution in your case is to just simulate events 'data'

. Each thread is an EventEmiiter, so the following will be done:



process.stdin.resume();
process.stdin.on("data", function(data) 
   console.log("recieved " + data)
});

process.stdin.emit('data', 'abc');

      

+3


source







All Articles