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!
- Which is the Best Operating System for Programming? Here are 3 OS to Consider - August 24, 2024
- Top 10 Cybersecurity Threats to Watch Out for in 2024 - August 17, 2024
- How Much Do Ethical Hackers Earn in India Per Month (Salary Guide) - August 11, 2024