Skip to Main Content

Kelbe Fox ID226

Advanced Web Scripting

Events Exercise

Node.js has the capability of creating events. When Node starts its server, it initializes the variables, declares functions, and then waits for the event to be triggered. When the event is triggered, the process is referred to as a an event loop. Node uses a class called EventEmitter from the Events module. This listens for events which can then trigger a callback function. The callback function produces the results we want. When you have multiple callbacks associated with one event, it initializes each function one at a time, one after the other. These are known as asynchronous tasks. The EventEmitter is a class from the Events module. This class allows us to raise and handle custom events.

In this exercise, we used event emitters along with user input to get results in the Command Prompt Console.

Below is a screenshot of a script with events called kfox_index.js.

When we run the script in the console, it makes calculations using the numerical input from the script, and prompts the user for input in the console.

After enterint input in the console, the script finishes running.