i

Event Handling

Event handing

Node.js has a built-in module called events for event handling. The entire event handling in Node.js will be handled by a built-in class called EventEmitter

EventEmitter is responsible for handling the events,  or we can say emitting the event.

Whenever an event is emitted, there should be an event handler function that is responsible for handling the event. 

EventEmitter class has many events and methods for Event Handling.

Few important ones being

Methods

1. On event

2. Emit event

On event is used to bind an event with an event handler function.

Emit event emits an event or trigger an event.

Events

1. Data event

2. End event

Data event is executed when data flows from the request.

End event gets executed when data fetching from the client in the server gets completed.
postRequestDemo.js
 

Running the server

As it is a POST request, data will not be sent via url but through form or POSTMAN client. So, we created a form here.

Form.html

As button fires POST request and it should be sent to local server with PORT 5500.

Running the form on the browser, which eventually calls server codes.

If the Username and Password mentioned is according to authenticateUser function.