博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Node.js] Level 2 new. Event
阅读量:6515 次
发布时间:2019-06-24

本文共 3663 字,大约阅读时间需要 12 分钟。

Chat Emitter

We're going to create a custom chat EventEmitter.

Create a new EventEmitter object and assign it to a variable called 'chat'.

var chat = new EventEmitter();

Next, let's listen for the 'message' event on our new chat object. Remember to add a callback that accepts the message parameter.

chat.on('message', function(message){});

Log the message to the console using console.log().

chat.on('message', function(message){    console.log(message);});

 

var events = require('events');var EventEmitter = events.EventEmitter;var chat = new EventEmitter();chat.on('message', function(message){    console.log(message);});

 

Emitting Events

Read the existing code below and modify it to emit events.

On the chat object, emit the 'join' event and pass in a custom message as a string.

// Emit events herechat.emit('join', "Hello");

Now emit the 'message' event on the chat object. Just like before, remember to pass in a custom message as a string.

chat.emit('message', "Message: ");

 

var events = require('events');var EventEmitter = events.EventEmitter;var chat = new EventEmitter();var users = [], chatlog = [];chat.on('message', function(message) {  chatlog.push(message);});chat.on('join', function(nickname) {  users.push(nickname);});// Emit events herechat.emit('join', "Hello");chat.emit('message', "Message: ");

 

Request Event

Just like you saw in the video, refactor the HTTP server code to explicitly bind a callback to the 'request' event using the onfunction.

Add an event listener on the server variable that listens to the requestevent. The event listener should take a callback function with two arguments, request and response.

server.on('request', function(request, response){});

Move the logic for handling the request from the http.createServer()callback to your new 'request' event listener. Remember to remove thehttp.createServer() callback once the code has been moved.

var server = http.createServer(function(request, response){    response.writeHead(200);    response.write("Hello, this is dog");    response.end();});//change to var server = http.createServer();server.on('request', function(request, response){    response.writeHead(200);    response.write("Hello, this is dog");    response.end();});

Listening Twice 

Who said you can only listen for an event once?

Add a second 'request' handler to the HTTP server.

server.on('request', function(request, response){});

From inside of the new handler, log the message "New request coming in..." using console.log().

var http = require('http');var server = http.createServer();server.on('request', function(request, response){  response.writeHead(200);  response.write("Hello, this is dog");  response.end();    });server.on('request', function(request, response){    console.log("New request coming in...");});server.listen(8080);

 

Listening for Close

Like our parents always used to say, listening is more important than talking! Modify the server so that we know when it's closed down.

Listen for the 'close' event on the server. The event listener should take a callback function that accepts no arguments.

server.on('close', function(){});

Inside the 'close' callback, log the message "Closing down the server...".

server.on('close', function(){    console.log("Closing down the server...");});

 

var http = require('http');var server = http.createServer();server.on('request', function(request, response) {  response.writeHead(200);  response.write("Hello, this is dog");  response.end();});server.on('request', function(request, response) {  console.log("New request coming in...");});server.on('close', function(){    console.log("Closing down the server...");});server.listen(8080);

 

 

转载地址:http://thafo.baihongyu.com/

你可能感兴趣的文章
AWK 简明教程
查看>>
smarty只能更包含include的困惑
查看>>
mysql架构
查看>>
linux导出>>文件到Window txt乱码
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
extreme (思路讯)交换机 配置vlan
查看>>
LINUX信息安全系统设计基础第一周学习总结
查看>>
几个网络测试命令
查看>>
syslog-ng应用详解
查看>>
Cisco ASA Failover
查看>>
SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)
查看>>
【非专业前端】vue+element+webpack
查看>>
产品经理的自我修养
查看>>
我的友情链接
查看>>
android多选联系人实现
查看>>
再读PE(2)
查看>>
老男孩Linux学习 -- 关于Linux网络
查看>>
我的友情链接
查看>>
System Center 2012 SP1 Data Protection Manager 设置备份
查看>>