Detailed explanation of the event handling mechanism in node. js

Source: Internet
Author: User
Details about the event processing mechanism in node. js EventEmitter class

The EventEmitter class is defined in the event module used by Node. js to implement various event processing. All objects that may trigger the event are instance objects that integrate EventEmitter classes. in js, many methods are defined for the EventEmitter class. All the methods associated with the binding and unbinding of event processing functions of objects are called for execution.

EventEmitter class methods

Event: indicates the event name.

Listener: indicates the event processing function.

The parameter in brackets indicates that this parameter is optional.

On Method of EventEmitter class

var http = require("http");var server = http.createServer();server.on("request", function(req, res){ console.log(req.url); res.end();});server.listen(1337, "127.0.0.1");

In this Code, specify the target URL of the client request in your console window when the server receives the client request, and end the response immediately using the end method of the response object.

Run the code and enter http: // localhost: 1337: // in the browser window. The console output is as follows:

Of course, you can also bind multiple event handler functions to the same event by executing multiple on methods. As follows:

Var http = require ("http"); var server = http. createServer (); server. on ('request', function (req, res) {console. log ('receive client request')}) server. on ("request", function (req, res) {console. log ('process client requests') console. log (req. url); res. end () ;}) server. on ('request', function (req, res) {console. log ('sent response')}) server. listen (1337, "127.0.0.1 ");

OK, run the code. The console output is as follows:

The once method of the EventEmiiter class is similar to the on method, and serves to bind an event handler function to a specified event. The difference is that the event handler function is immediately exposed after it is executed, that is, the event processing function is executed only once. The parameters used by the once method are the same as those used by the on method, as follows:

emitter.once(event, listener)

Do a test.

Run the following code (same as above ):

Var http = require ("http"); var server = http. createServer (); server. on ('request', function (req, res) {console. log ('receive client request')}) server. on ("request", function (req, res) {console. log ('process client requests') console. log (req. url); res. end () ;}) server. on ('request', function (req, res) {console. log ('sent response')}) server. listen (1337, "127.0.0.1 ");

Then, open 127.0.0.1: 1337 twice in a browser window. The console output is as follows:

Then, modify the on event to a once event. The Code is as follows:

Var http = require ("http"); var server = http. createServer (); server. once ('request', function (req, res) {console. log ('receive client request')}) server. on ("request", function (req, res) {console. log ('process client requests') console. log (req. url); res. end () ;}) server. once ('request', function (req, res) {console. log ('sent response')}) server. listen (1337, "127.0.0.1 ");

The console output is as follows:

The Code is as follows:

Var http = require ("http"); var server = http. createServer (); var testFunction = function (req, res) {console. log ('sent response')} server. on ('request', function (req, res) {console. log ('receive client request')}) server. on ("request", function (req, res) {console. log ('process client requests') console. log (req. url); res. end () ;}) server. on ('request', testFunction) // Delete server. removeListener ('request', testFunction) server. listen (1337, "127.0.0.1 ");

Run the code and enter 127.0.0.1: 1337 in the browser window. The console output is as follows:

The Code is as follows:

Var http = require ("http"); var server = http. createServer (); server. on ("request", function (req, res) {console. log (req. url) ;}); // Custom Event server. on ("customEvent", function (arg1, arg2, arg3) {console. log ("Custom event triggered"); console. log (arg1); console. log (arg2); console. log (arg3) ;}); // triggers the Custom Event server. emit ('customent', 'custom parameter 1', 'custom parameter 2', 'custom parameter 3') server. listen (1337, "127.0.0.1 ");

If you do not enter an address in the browser window this time, run the code to view the console output. The console output is as follows:

This means that we have manually triggered a custom event, that is, customEvent.

Summary

The above is all the content of this article. I hope the content of this article will be helpful for everyone to learn or use node. js.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.