Node functions process

I don’t understand what are those functions from node, I have to run any code inside those functions, but I don’t know how:

process.stdin?
.on (“data”, function (input){?
process.stdin.on (“end”, function (){?

function main (input) {
 input.split ("\n").forEach (function (n){
     console.log (n);
});
}
process.stdin.resume ();
process.stdin.setEncoding ("utf-8")
process.stdin.on ("data", function (input){
        stdin_input += input;
});
process.stdin.on ("end", function (){
      main (stdin_input);
});
1 Like

I’m just starting Node today myself, so I don’t quite understand your question, but maybe these are helpful:
process.stdin:
https://nodejs.org/api/process.html#process_process_stdin , and
https://en.wikipedia.org/wiki/Standard_streams

If you don’t have https://nodejs.org/api/ bookmarked, that’s a good idea. And personally I’m (as in like half an hour ago) finding this video series helpful: https://www.youtube.com/playlist?list=PL4cUxeGkcC9gcy9lrvMJ75z9maRw4byYp

1 Like

process.stdin is Node’s way of getting data that’s passed into it from outside. stdin, short for “standard in”, is the path by which we can pass data into an application. This is usually text data that the user has typed, but it can also come from another application. A simple example of this would be:

$: cat "This is some text!" | node app.js

cat is a unix program that simply combines text from one or more files (or from directly typing in some text, as above), and spits out the result. The vertical bar, or pipe operator, means that we’re connecting the output from cat to the input of node app.js. Whatever is output from cat will be available as the stdin of app.js.

As you’ve noticed, the way we get this data is a little strange. The process.stdin object has the function .on(), which lets us add event handlers just like you would in the browser. Remember jQuery?

$('#button').on('click', function(event) {
    console.log("You clicked a button!")
})

What you’re seeing is an event handler for the data event.

process.stdin.on ("data", someFunction);

In plain English, this can be read as, when process.stdin receives some data, perform someFunction(). In your case, it’s adding the text coming in from stdin to a variable called stdin_input. Later,

process.stdin.on ("end", someOtherFunction)

This says, when process.stdin receives the signal that there’s no more input, perform someOtherFunction().

2 Likes

Thank you, now I am starting to understand.

So if I create a function sumNumbers and put in process.stdin.on(“end”, sumNumbers) will execute that function and inside data is the function sumNumbers a + b ?

It will invoke someNumbers when input has stopped. a and b will have to be defined somewhere, but it will work.

Thanks. I will read and watch the video.

Something like this code :

The problem here is that someNumbers needs to get a and b as parameters. That isn’t something sent by the “end” event.

Where did you get this code from? Is this a tutorial somewhere?

This code was pass like a template in a test, it was confusing because I need to put code inside that function, but I wasn’t understanding.