Events and Event Emitter in nodejs
This blog we have seen mongodb basic and how is it
work with installation and after installation, we have already seen
mongodb basic, CRUD operation, connect mongodb with nodejs, read, update,
delete, POST, GET API etc.
we should not face
any problem while making Database, collection(table) and using the data in nodejs even whenever we will make
API. Read data from mongodb in nodejs
- # What is Mongoose
-
# Install Mongoose
-
# What is Events and Event Emitter
-
# Make an event and call it
Now in this blog we will use mongoose. There is an
npm package to connect nodes to mongodb. Mongoose is a MongoDB object modeling tool designed to work
in an asynchronous environment. They created mongoose by improving
mongodb, with the help of which you can create schemas and models of mongoose's
movements.
Suppose that there are 4 fields in our database (like name, brand, price, category) and the user wants to add a 5th field also (like color) but we want
to add only 4 fields, for this we create schemas.
Here you can also define TYPE in schemas like price type is
numeric but if any user wants to use quotes (" " ' ') then we can stop the
user for this.
in sort we can apply validation in mongoose but we
can't in directly mongodb.
What is MongoDB
MongoDB is a popular open-source NoSQL (non-relational) database management system. NoSQL means no structure like it is look like an object while in SQL it is a structure query language like SELECT CustomerName, City FROM Customers; (The SELECT statement is used to select data from a database.) But NoSQL database is useless...NOT completely because of whenever we want add extra column in database then it is possible through NoSQL MongoDB.
MongoDB installation - CLICK HERE
MongoDB Basic Command -
CLICK HERE
About MongoDB vs SQL - CLICK HERE
CRUD in MongoDB - CLICK HERE
Object Like -
{ "_id": { "$oid": "659ad46e0382148123c82841" }, "name": "moto G60", "brand": "motorola", "price": 16500, "category": "mobile" }
MongoDB is a type of database that helps store and manage large amounts
of data in a flexible and scalable way. Unlike SQL, MongoDB doesn't require a
fixed structure for the data, allowing you to store information in a more
versatile format.
- MongoDB is a NoSQL database.
- The data stored in a collection
- Collection don't have row and columns
- Data is stored in the form of object.
Install Mongoose
For this you need to copy this (npm i mongoose) and paste it in project's
terminal.
What is Events and Event Emitter
In nodejs, an event is an action or occurrence that can be observed and responded to . Node.js provides the event emitter pattern as a core module to handle events. The Event emitter is an implementation of the observer pattern allowing objects (event emitter) to emit the named events that cause function objects (listeners) to be called.
OR
if i will use button in HTML and add the onClick event and whenever we
click on button and generate the function and get a signal that please call
the function and execute the code.
and the event is that which observes the singal received from the event and works on that pattern.
Basic Usage:
To use the Event Emitter, you first create an instance of Event
Emitter.
You can then use the .on(eventName, listener) method to attach a listener function to a specific event.
The
.emit(eventName, ...args) method is used to emit (trigger) an event,
and any attached listeners for that event are called.
Q) How can we make a button in Node.js ?
Ans - Through API and use Event Emitter
Make an event and call it
First we will make a API and import express and EventEmitter (EventEmitter is inbuilt module in nodejs) so don't need to install in nodejs.
const express = require('express') const EventEmitter = require('events'); const app = express() app.get("/", (req, resp) => { resp.send("Done") console.log("API called") }) app.listen(8000)
Now we will see when we create so many API then we will check how many hit on particular API.
app.get("/", (req, resp) => { resp.send("Home page API") console.log("Home page API called") }) app.get("/search", (req, resp) => { resp.send("Search API") console.log("Search API called") }) app.get("/update", (req, resp) => { resp.send("update page API") console.log("update API called") })
Now we make a function that means it will keep updating the count of every hit we get on our API in the database.
const express = require("express"); const EventEmitter = require("events"); const app = express(); const event = new EventEmitter(); //this is object let count = 0; event.on("countAPI", () => { count++; console.log("Event called", count); }); //first is the event name - countAPI //second is the callback function app.get("/", (req, resp) => { resp.send("Home page API"); event.emit("countAPI"); }); app.get("/search", (req, resp) => { resp.send("Search API"); event.emit("countAPI"); }); app.get("/update", (req, resp) => { resp.send("update page API"); event.emit("countAPI"); }); app.listen(8000);
Disclaimer
All tutorials are for informational and educational purposes only and have been made using our own routers, servers, websites and other vulnerable free resources. we do not contain any illegal activity. We believe that ethical hacking, information security and cyber security should be familiar subjects to anyone using digital information and computers. Hacking Truth is against misuse of the information and we strongly suggest against it. Please regard the word hacking as ethical hacking or penetration testing every time this word is used. We do not promote, encourage, support or excite any illegal activity or hacking.
0 comments:
Post a Comment
For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.