By: William Alexander in node.js Tutorials on 2018年08月04日 [フレーム]
In this tutorial we will see how node.js can be used to build an event driven application. It is assumed that you have node.js installed as well as an IDE to write and organize your code. If not it is highly recommended that you see this tutorial on how node.js can be used as a HTTP server which is an introduction to node.js and a getting started guide for node.js
In olden days when there was no GUI, the programs normally were run sequentially in a blocking manner. But with GUI, the event driven programming became popular as events were generated by the user in the form of mouse clicks. So the user has complete control of what to do next by navigating randomly within an application by invoking any menu or feature within the application. Each mouse click therefore is an event and based on that event a particular code will get executed. Hence in an event driven programming, there will be a main thread which listens to events and for each event there will be an event handler (code) that gets executed. Events therefore could be generated or triggered by an user interaction or by another event or by a hardware event etc..
Traditionally, server programs will use multi threading to handle concurrency. For example there will be a main loop which listens continuously for events to occur and for each event, there will be a dedicated child thread that handles the request by executing the event handler. Once the event handler is executed, the program returns back to the main loop. But each individual child thread is still blocking in the sense it will have to completely execute the I/O operations before exiting the thread.
Whereas in node.js, it is completely non-blocking because it uses JavaScript at the core. If you look at JavaScript's history, it was invented in 1995 to execute code within netscape browser on the client side. Because of this, JavaScript did not have any I/O capability as it just ran in client browser and cannot access a local file. And it was asynchronous and was created from the ground up as an event driven programming language, to handle events such as user clicking on a html form, or a page loading event etc. So when node.js developers decided to use JavaScript for node.js, they had to write the I/O libraries from scratch and they implemented a non-blocking I/O capability on the server side by dedicating a pool of thread that is dedicated to I/O.
So all you need to know is that node.js is single threaded, light weight, even-drivern, asynchronous, non-blocking server side application development platform. And it is perfect for event-driven programming applications.
As discussed earlier, all events have to have an event handler and in node.js this is done using the package named 'events'.
Create a file named 'eventdriven.js' and copy the below code:
var events = require('events');
var eventEmitter = new events.EventEmitter();
//Create an event handler:
var sampleEventHandler = function () {
console.log('This is a sample event handler!');
//You can write other code to execute when this event occurs
}
//Assign the event handler to an event:
eventEmitter.on('sampleevent', sampleEventHandler);
//Fire the 'sampleevent' event:
eventEmitter.emit('sampleevent');Here we are just creating an event handler for the event 'sampleevent'. And an on function that maps the event to its event handler. And in our case the event handler just logs a message.
You can test this script by executing 'node eventdriven.js'
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in node.js )
Node.js as a HTTP Server - Building from scratch - Tutorial for Beginners
Is it safe to run 'npm audit fix'?
Send email from node.js application
package.json vs package-lock.json in react-native project
Latest Articles (in node.js)
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate