Page 1 of 1

Making a career transition can be challenging, with a lot of uncertainty and that feeling of starting from scratch. Even

Posted: Sun Feb 02, 2025 3:53 am
by monira444
In this article, we will use Node.js with the NestJS framework to demonstrate the use of events, an efficient way to handle communication between different parts of an application. Let's create an example to better understand its use.

The API will receive user data to register a user in the example scenario. After registration, the user will receive two emails: a registration confirmation email and another containing a link to create a new password.

Setting up the project
First, if you don't have the Node.js environment installed, follow the steps in the article installing node.js . Then install the NestJS CLI, using the following command:

Perfect, the structure and files will be generated. For the example, we will simply create the User interface, to simulate user registration. When “registering” this user, the application will use the events to trigger the emails as explained above. For this, we will have the following implementations:

We use the default controller generated by NestJS, but we chinese america data change the Get route to Post, where we will receive a user in the request and call the createUserservice layer method, passing the user as a parameter.

Note that we simply returned the “registered” user. It is important to note that, in a real project, this user would be saved in the database. However, for educational purposes, we will now focus on using events to send emails.


Creating the event emitter
Finally, we can configure the event that we will use for our example. The first step is to install the package event-emitterfor NestJS and create the service that we will use to create the event emitter with the following commands:

After installing event-emitter, we will import it into the main application module:

nest generate service event-emitter
Nest will create the directory event-emitter, to make the example easier, we will rename the folder to eventsand delete the test file that will be generated within this directory (the .spec file), then we will create the event emitter, that is, the method that will emit the event and trigger the actions in the file event-emitter.service.ts:

Note that we used dependency injection to access the eventEmitter, we created the method sendNewUserEmails(), this method will execute the event.emit(), in this way we can pass as a parameter the string related to the event name, in this case: user.created, which we passed as the first parameter. Another point is that we passed the object useras the second parameter, as it will be used in the events that will be triggered.

Now we need to create the events that will be triggered when the event.emit()is executed, for this we use the decorator @OnEvent()and pass the event that was described in event.emit()which in this case is the event user.created.

We implemented two methods related to a single event, that is, when a user is registered, the application will send two emails, one about the registration confirmation and another for the creation of a password. Finally, we need to call the email sending event in the service

Let's run the application with the command npm run start:devand send a request to the post route that was created in the controller, containing a user's data:

registering user to trigger events

Perfect, we received the 201 created response and the data of the “registered” user. In the example, we did not implement the actual sending of emails, but simulated the sending of these emails with two messages that will be displayed in the console when the event is triggered. When consulting the terminal, we have the following result:

The interesting thing is that, with this structure where events are triggered when a user is registered, it allows our application to be more independent. We can control the events that will be triggered when a user is registered without having to change the method createUser(). For example, if in the future it is necessary to implement a feature where the registered user must be sent as a “lead” to a CRM, we can implement it independently and use the decorator @OnEvent()to observe when the event user.createdis called without changing any other part of our application related to user registration.