Skip to content

Instantly share code, notes, and snippets.

@beatak
Created October 9, 2015 22:36
Show Gist options
  • Save beatak/76b4dd81270606143b3a to your computer and use it in GitHub Desktop.
Save beatak/76b4dd81270606143b3a to your computer and use it in GitHub Desktop.
how to receive stdin in node
var onend = function (val) {
console.log('hi');
if (val) {
console.log('received:');
console.log(val);
}
process.exit(0);
};
if (process.stdin.isTTY) {
onend();
}
else {
process.stdin.setEncoding('utf8');
var lines = [];
process.stdin.on(
'data',
function(chunk) {
lines.push(chunk);
}
);
process.stdin.on(
'end',
function() {
onend(Buffer.concat(lines).toString('utf8'));
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment