FAQ: Node.js Essentials - User Input/Output

This community-built FAQ covers the “User Input/Output” exercise from the lesson “Node.js Essentials”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Node.js

FAQs on the exercise User Input/Output

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

in ex.3 in the end I wrote “node app.js” and got an error

Me too. I literally have no idea what’s going on. This node.js minicourse is impossible to understand.

1 Like

I agree… This course is quite hard to understand even though I’m doing it slowly. We need more simplified explanations.

But I still wanna complete it anyway.

process.stdin.on(‘data’, playGame);

Why is the first of two entries for process.stdin.on, ‘data’ ? I cant find another ‘data’ anywhere in app.js or game.js. I checked on line and any examples I can find, they just use ‘data’ is well. is this merely convention or is it referencing another ‘data’ somewhere else in ‘process.’ ? What other entries can be used instead of ‘data’ and what do they do?

5 Likes

One thing that was helpful for me was to watch Crash Course computer Science - it explains the background that is being used in node.js and other backend applications of languages.

Why does the game keep looping if we get the wrong number? And how does Node know to accept another number and to only quit program when we type quit of get the number correct?

I’m expecting to see some kind of a loop or more code that tells node to do that explicitly, but I don’t see it. I would expect to see it in the last if statement of the games.js else function. What an I missing or not understanding?

Why should I have to pass the function without invoking it (without the parentheses) to process.stdin.on('data', playGame);

Hi… how come process.stdin is an instance of EventEmitter?? i thought EventEmitter is in event module not in the process module?? thank you in advance

process.stdin.on is a HOF… a function that takes a function as an argument, that why we don’t need to invoke a function when it is an argument

Yeah they could have introduced event types at this point to avoid a lot of confusion for us… Took me a lot of digging to find, and it doesn’t seem that the node documentation has a reference for all event types, they are scattered around under each applicable module. ‘Data’ is a ‘stream’ module event of class ‘readable.’ Here’s the link:

https://nodejs.org/api/stream.html#event-data

Hope this saves someone in the future from this headache, lol.

2 Likes

So event names are limited to a specific set of keywords, then? Or do specific event names have special effects? Because the previous lesson created an event emitter with the name “celebration” so I figured you could name it whatever you wanted. But that was also attached to declaring an eventEmitter, not a process.stdin.

Hopefully this begins to make sense later on down the line; it seems like I’m missing fundamental information…

I could be wrong, I’m in the learning stage myself, and this is what I could figure out looking into it since it wasn’t really explained. I think the answer is both? For example, the doc for the events module lists two events for EventEmitter, ‘newListener’ and ‘removeListener.’ Those would be its built-in events. But the EventEmitter class allows you to create your own custom events with the emit() method, and handle them with on(). Here’s an example I took from tutorials teacher:

var emitter = require('events').EventEmitter;

function LoopProcessor(num) {
    var e = new emitter();
    
    setTimeout(function () {
        
        for (var i = 1; i <= num; i++) {
            e.emit('BeforeProcess', i);
            
            console.log('Processing number:' + i);
            
            e.emit('AfterProcess', i);
        }
    }
    , 2000)
    
    return e;
}
var lp = LoopProcessor(3);

lp.on('BeforeProcess', function (data) {
    console.log('About to start the process for ' + data);
});

lp.on('AfterProcess', function (data) {
    console.log('Completed processing ' + data);
});

Here they defined two events, ‘BeforeProcess’ and ‘AfterProcess’ with emit(), and defined how to handle them with on(). I think that node gives you the ability to create custom events as needed, but since many types of events will be common place, they have already defined those and provided event handler methods for you.

Like I said I could be wrong, and if anyone else knows better than I do, I’d love to hear it, but I’ve noticed the lesson FAQ’s don’t seem to get too much attention, unfortunately.

Thanks for looking into it! My suspicion is that, yes, you can provide whatever name you want to events when you create event emitters, but process.stdin has only a specific set of accepted names because it expects only a specific set of input types and that name serves to determine how it should behave.

process.stdin.on('data', callbackfunction) is basically an event listener for receiving ‘data,’ so that’s why you can’t just write whatever you want there. It’s like adding an onSpaghetti event listener in the browser; doesn’t mean anything and the browser isn’t equipped to respond to spaghetti.

2 Likes

Hi guys. Just read the documentation at node.js for process.stdin, which returns a Stream(net.Socket) which have the ‘data’ listener and ‘data’ would be a Buffer or String based on the documention, resulting in the need for toString function. Thats my best guess. Anyone with a better answer feel free to add on. Cheers!

Node.js doc -Found at Process | Node.js v21.1.0 Documentation
net.Socket listner list -Found at Net | Node.js v21.1.0 Documentation

This exercise is poorly explained and took me a while to understand. It seems there is an event with a specific name called ‘data’ that is emitted when a user inputs something and hit enter, but it is not explained that the ‘data’ is a specific name.