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);

[Node.js] Level 2 new. Event的更多相关文章

  1. Node.js 事件循环(Event Loop)介绍

    Node.js 事件循环(Event Loop)介绍 JavaScript是一种单线程运行但又绝不会阻塞的语言,其实现非阻塞的关键是“事件循环”和“回调机制”.Node.js在JavaScript的基 ...

  2. [Node.js] Level 7. Persisting Data

    Simple Redis Commands Let's start practicing using the redis key-value store from our node applicati ...

  3. [Node.js] Level 6. Socket.io

    6.2 Setting Up socket.io Server-Side So far we've created an Express server. Now we want to start bu ...

  4. [Node.js] Level 3 new. Steam

    File Read Stream Lets use the fs module to read a file and log its contents to the console. Use the  ...

  5. [Node.js] Level 5. Express

    Express Routes Let's create an express route that accepts GET requests on'/tweets' and responds by s ...

  6. 浅析Node.js的Event Loop

    目录 浅析Node.js的Event Loop 引出问题 Node.js的基本架构 Libuv Event Loop Event Loop Phases Overview Poll Phase The ...

  7. Node.js Event Loop 的理解 Timers,process.nextTick()

    写这篇文章的目的是将自己对该文章的理解做一个记录,官方文档链接The Node.js Event Loop, Timers, and process.nextTick() 文章内容可能有错误理解的地方 ...

  8. Node.js Web 开发框架大全《中间件篇》

    这篇文章与大家分享优秀的 Node.js 中间件模块.Node 是一个服务器端 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用程序,编写能够处 ...

  9. Node.js异步处理CPU密集型任务

    Node.js异步处理CPU密集型任务 Node.js擅长数据密集型实时(data-intensive real-time)交互的应用场景.然而数据密集型实时应用程序并非仅仅有I/O密集型任务,当碰到 ...

随机推荐

  1. 【Vijos 1998】【SDOI 2016】平凡的骰子

    https://vijos.org/p/1998 三维计算几何. 需要混合积求四面体体积: 四面体剖分后合并带权重心求总重心: 四面体重心的横纵坐标是四个顶点的横纵坐标的平均数: 三维差积求平面的法向 ...

  2. 20162318 2018-2019-2《网络对抗技术》Exp1 PC平台逆向破解

    一.实验目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getS ...

  3. C++11中的raw string literals

    作为一名C++书看得少得可怜的新手,我一直没有勇气去系统地学习一下C++ 11添加的新特性.不过,平日里逛论坛,阅读大犇们的博客,倒是了解了一些.比如,这个帖子: 如何绕过g++ 4.8.1那个不能在 ...

  4. JDK源码学习笔记——Enum枚举使用及原理

    一.为什么使用枚举 什么时候应该使用枚举呢?每当需要一组固定的常量的时候,如一周的天数.一年四季等.或者是在我们编译前就知道其包含的所有值的集合. 利用 public final static 完全可 ...

  5. Alpha冲刺(2/10)——追光的人

    1.队友信息 队员学号 队员博客 221600219 小墨 https://www.cnblogs.com/hengyumo/ 221600240 真·大能猫 https://www.cnblogs. ...

  6. mysql慢查询配置

    1.慢查询有什么用? 能记录下所有执行超过long_query_time时间的SQL语句, 帮你找到执行慢的SQL, 方便我们对这些SQL进行优化. 2. 如何开启慢查询? 首先我们先查看MYSQL服 ...

  7. Notepad++源代码阅读——窗口元素组织与布局

    1.1 前言 这两天在看notepad++ 1.0版本的源代码.看了许久终于把程序的窗口之间的关系搞清楚了现在把其组织的要点写于此,希望对大家有所帮助. 1.2 窗口元素之间的关系 Notepad++ ...

  8. Google的Shell开发规范

    官方:https://google.github.io/styleguide/shell.xml 中文: http://zh-google-styleguide.readthedocs.io/en/l ...

  9. 原生js实现图片预览并上传

    最近主导的PC客户端网站重构工程告一段落,下一阶段开始给公司APP开发H5页面,技术栈是react.最近碰到一个需求:需要在H5页面上添加身份证照片,预览并上传.因为要兼容安卓4.4以下版本的手机,所 ...

  10. 【转】利用HTML5开发Android

    ● Android设备多分辨率的问题 Android浏览器默认预览模式浏览 会缩小页面 WebView中则会以原始大小显示 Android浏览器和WebView默认为mdpi.hdpi相当于mdpi的 ...