Site icon Mr Programmer

Events In NodeJS + NodeJS Project | Node.Js Tutorial #4

Events In NodeJS + NodeJS Project | Node.Js Tutorial #4

Hey Programmers, In Prevois Three Parts Of NodeJS We Covered About NPM, Modules In NodeJS, And We Also Created a Web Server Project In NodeJs, If you Have Missed The Previous Parts Of this Tutorial You can Access Them Here: NodeJS Tutorial

Table Of Contents:

  • Events In NodeJS
  • Events Module In NodeJS
  • EventEmitter Object In NodeJS
  • Buidling Project: Emai Sender NodeJS

Events In NodeJS

Every Action Performed On a Computer is a Event. For Example You Opened a Folder or File In Your Computer That is a Event. Objects In NodeJS Fire Events Like ReadStream Which Reads Any File Using NodeJS.

// File System Module
var fs = require('fs');
var rs = fs.createReadStream('./new.txt');
rs.on('open', function(){
console.log("The File is Open!");
});

Events Module In NodeJS

In the Above Example, We Saw the Events In NodeJs, But NodeJS Also Comes With a Built-In Module For Firing Events In NodeJS. To Use the Events Module Just Use the require() Function.

var events = require('events');
var eventEmitter = new events.EventEmitter();

//  Creating a Event Handler
var EventHandler = function(){
    console.log("Hello This is a Event");
}
// Assgining Event to a Event Handler
eventEmitter.on('Hello', EventHandler);

// Firing the Assigned Event
eventEmitter.emit('Hello');

EventEmitter Object In NodeJS

We Use EventEmitter Object to Assign Event Handlers To Our Events In NodeJS. In the Example Below We can Clearly See that We have Created Function that will Give the Output “Hello” Event is Fired. To Fire a Event In NodeJS We Use emit().

var events = require('events');
var eventEmitter = new events.EventEmitter();

//  Creating a Event Handler
var EventHandler = function(){
    console.log("Hello This is a Event");
}
// Assgining Event to a Event Handler
eventEmitter.on('Hello', EventHandler);

// Firing the Assigned Event
eventEmitter.emit('Hello');

Buidling Project: Emai Sender NodeJS

Now, We will Build a Project Using NodeJS, Which Sends Emails Or In Other Words Automates Your Emails. For This Project We Need a NodeJS Module Called “nodemailer“.

npm install nodemailer

Email Sender

// Importing Nodemailer Into the mail.js
var nodemailer = require('nodemailer');

var Transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'youremail@gmail.com',
        pass: 'YourPasswordHere'
    }
});
var mailOptions = {
    from: 'youremail@gmail.com',
    to: 'sendersmail@gmail.com',
    subject: 'Node.Js',
    text: 'Hi this mail is Send Using Node.js',
};
Transporter.sendMail(mailOptions, function (error, info) {
    if (error) {
        console.log(error);
    } else {
        console.log('Your Email was Sent!', + info.response);
    }
});

Conclusion

From this We Come to The End of NodeJS Tutorial, So This Was For This Blog See you In the Next One Till Keep Coding Keep Exploring!

NodeJS Complete Tutorial

Author

  • Founder & CEO of MrProgrammer.in | I help Learning Freaks to Step into the World of Programming and Our Mission is to Provide High Quality and Valuable Content to Our Users! We believe that everyone has the potential to learn to code, and we are committed to making that happen. Our courses are designed to be engaging and interactive, and we offer a variety of learning paths to meet the needs of all learners. We are also committed to providing our learners with the support they need to succeed, and we offer a variety of resources to help learners learn at their own pace. If you are interested in learning to code, we encourage you to check out our courses. We are confident that you will find our content to be high-quality, valuable, and makes sense.

Exit mobile version