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 here
chat.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 here
chat.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);

最新文章

  1. Redis系列-好玩的用法
  2. Linux服务器宕机案例一则
  3. SQL Server 数据库查找重复记录的几种方法
  4. cetnos 7 ntp服务的安装与配置
  5. java 28 - 7 JDK8的新特性 之 接口可以使用方法
  6. jQuery如何动态添加具有删除按钮的行
  7. 知方可补不足~SQL数据库用户的克隆,SQL集群的用户同步问题
  8. 单点登录CAS使用记(七):关于服务器超时以及客户端超时的分析
  9. C语言学习 —— 字符串的学习(一)
  10. 优酷m3u8视频源地址获取失败
  11. MyBatis学习(六)MyBatis关联映射之一对多映射
  12. Java中使用LocalDate根据日期来计算年龄
  13. winform自动更新程序实现
  14. python selenum 爬取淘宝
  15. Lab 1-4
  16. wordApp.Documents.Open 未将对象引用实例
  17. NOI.AC NOIP模拟赛 第六场 游记
  18. mybatis学习笔记(三)-- 优化数据库连接配置
  19. 你的centos/linux下有多个php.ini,不确定是哪个时
  20. 部署maven的一些要点、遇到的问题

热门文章

  1. 下载 ....aar jitpack.io 打不开。
  2. NEUQ OJ 2004:追梦之人 (计数数位dp)
  3. poj 3164
  4. UVALive 5058 Counting BST 数学
  5. Codeforces Round #279 (Div. 2) C. Hacking Cypher 机智的前缀和处理
  6. 转:Windows中的命令行提示符里的Start命令执行路径包含空格时的问题
  7. linux 内核升级 网址参考
  8. DM6446开发攻略:UBOOT-2009.03移植及nand flash烧写
  9. C#中数据库连接的几种方式
  10. UITableView 让 cell 被选中的颜色底色快速消失,而不是一直停留在cell上