1

I want to send a buffer as a piped input to a node script on the terminal. Thus I created a script ping.js with the following code:

#!/usr/local/bin/node

function bufferDemo() {
  var objBuffer = new Buffer(29);
  objBuffer.writeUInt32LE(29,0);
  objBuffer.write('{message:"pingfrompingjs"}',4);

  return objBuffer;
}

bufferDemo();

Then I ran the following command on the command line:

 ./ping.js | ./index.js 

I also tried:

 ./ping.js > out.json

out.json is empty so obviously the ping.js is not passing the buffer. How do I achieve this? I am relatively new to node.

shashi
  • 4,616
  • 9
  • 50
  • 77
  • so as colleagues suggested use something like `console.log(buffer.toString('utf8'));` because `|` works with unix based `stdin` and `stdout`. so you want to pass data via that channel first. – Eugene Hauptmann Feb 06 '16 at 22:18

2 Answers2

2

returning from the function is no effect whatsoever. If you want your command to output something, you have to write to stdout (or stderr):

process.stdout.write(objBuffer);
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
2

You're not actually outputting anything by returning the value in your function. You should be writing to process.stdout:

process.stdout.write(objBuffer)
Wex
  • 15,539
  • 10
  • 64
  • 107