In node. js, many objects emit events. For example, an event is emitted when a file is opened by Fs.readstream.
All events are issued to the object. An instance of Eventemitter can be obtained by require ("event") and the event module.
The Listener function (listeners) can be added to an object, and the corresponding function is executed when the object emits an event. In the listener function, this refers to it (listener function)
First, class:events. Eventemitter
Through require (' events '). Eventemitter get Eventemitter class.
When an Eventemitter object encounters an error, the error event is usually triggered. The error event is a special case in Nodejs, which, if there is no listener, prints out the stack tracker and exits the program by default.
Second, add the listener
Bind event handlers for events, which can be used Emitter.addlistener (Event,listener) and Emitter.on (Event,listener), and they work exactly the same. The incoming parameters are events (event) and handler functions (listener).
Demo:test1.js content is as follows
var http = require (' http '); var server = http.createserver (); // binding handler functions for request events // You can also use Server.addlistener Server.on function (req, res) { res.writehead ($, {' Content-type ': ' Text/plain ' }); Res.write (' Shiyanlou '); Console.log (' Shiyanlou '); Res.end ();}); Server.listen (1337, ' 127.0.0.1 '); Console.log (' Server running at http://127.0.0.1:1337/');
Third, only the listener that executes once
An event listener that is bound with emitter.once (event,listener) is executed only once and then deleted.
Demo:test2.js content is as follows
var http = require (' http '); var server = http.createserver (); // to bind a handler function to the request event, the event is executed only once server.once function (req, res) { res.writehead ($, {' Content-type ': ' Text/plain ' }); Res.write (' Shiyanlou '); Console.log (' Shiyanlou '); Res.end ();}); Server.listen (1337, ' 127.0.0.1 '); Console.log (' Server running at http://127.0.0.1:1337/');
Iv. Removing listeners
Remove the listener using Emitter.removelistener (Event,listener);
Demo:test3.js content is as follows
varHTTP = require (' http ');varServer =http.createserver ();functionCallback (req, res) {Res.writehead ($, {' Content-type ': ' Text/plain ' }); Res.write (' Hello World '); Console.log (' Hello World '); Res.end ();} Server.on (' Request ', callback);//remove a bound listener callbackServer.removelistener (' request ' ) , callback); Server.on (' Request ',function(req, res) {Res.writehead ($, {' Content-type ': ' Text/plain ' }); Res.write (' Shiyanlou '); Console.log (' Shiyanlou '); Res.end ();}); Server.listen (1337, ' 127.0.0.1 '); Console.log (' Server running at http://127.0.0.1:1337/');
The run result shows only Shiyanliu and does not show Hello world because the "Hello World" listener is removed.
V. Remove all listeners
Remove all listeners using Emitter.removealllistener ([Event]).
Demo:test4.js content is as follows:
varHTTP = require (' http ');varServer =http.createserver (); Server.on (' Request ',function(req, res) {Res.writehead ($, {' Content-type ': ' Text/plain ' }); Res.write (' shiyanlou,111 '); Console.log (' shiyanlou,111 '); Res.end ();}); Server.on (' Request ',function(req, res) {Res.writehead ($, {' Content-type ': ' Text/plain ' }); Res.write (' shiyanlou,222 '); Console.log (' shiyanlou,222 '); Res.end ();});//Remove all the listeners that are boundserver.removealllisteners (' request ' )); Server.on (' Request ',function(req, res) {Res.writehead ($, {' Content-type ': ' Text/plain ' }); Res.write (' Shiyanlou '); Console.log (' Shiyanlou '); Res.end ();}); Server.listen (1337, ' 127.0.0.1 '); Console.log (' Server running at http://127.0.0.1:1337/');
Vi. Setting the listener maximum number of bindings
Emitter.setmaxlisstener (n) can set the maximum number of listeners for the same event, and by default, more than 10 are good warnings, which can help us quickly find out where the memory leaks. Obviously, not all event triggers are limited to 10 listeners, which can be set by this method, if set to 0 is unrestricted.
Vii. Custom Events
Use Emitter.emit (event,[arg1],[arg2],[...]) to trigger a custom event.
Demo:test5.js content is as follows
var http = require (' http '); var server = http.createserver (); // Binding Custom Event MyEvent function (ARG) { console.log (arg);}); // Triggering custom Events server.emit (' MyEvent ', ' Shiyanlou'); Server.listen (1337, ' 127.0.0.1 '); Console.log (' Server running at http://127.0.0.1:1337/');
The result of the run is output Shiyanlou in the console interface, indicating that the custom event was triggered successfully.
Eight. View the number of listeners for event bindings
Use Eventemitter.listenercount (emitter,event) to view the number of event listeners.
Demo:test6.js content is as follows
varHTTP = require (' http ');varEvents = require (' events ');//Loading Events ModulevarServer =http.createserver (); Server.on (' Request ',function(req, res) {Res.writehead ($, {' Content-type ': ' Text/plain ' }); Res.write (' shiyanlou,111 '); Console.log (' shiyanlou,111 '); Res.end ();}); Server.on (' Request ',function(req, res) {Res.writehead ($, {' Content-type ': ' Text/plain ' }); Res.write (' shiyanlou,222 '); Console.log (' shiyanlou,222 '); Res.end ();}); Server.listen (1337, ' 127.0.0.1 '); Console.log (' Server running at http://127.0.0.1:1337/');//to view the number of listeners for the server-bound ' request ' eventvarnum = events. Eventemitter.listenercount (server, ' request ')); Console.log (num);
Run the result output 2 because the server binds two listeners to the "Request" event.
The author starof, because the knowledge itself in the change, the author is also constantly learning and growth, the content of the article is not updated regularly, in order to avoid misleading readers, convenient tracing, please reprint annotated source: http://www.cnblogs.com/starof/p/5035522. HTML has a problem welcome to discuss with me, common progress.
Nodejs Module--event Module