[Node.js] Level 2 new. Event
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 on
function.
Add an event listener on the server
variable that listens to the request
event. 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的更多相关文章
- Node.js 事件循环(Event Loop)介绍
Node.js 事件循环(Event Loop)介绍 JavaScript是一种单线程运行但又绝不会阻塞的语言,其实现非阻塞的关键是“事件循环”和“回调机制”.Node.js在JavaScript的基 ...
- [Node.js] Level 7. Persisting Data
Simple Redis Commands Let's start practicing using the redis key-value store from our node applicati ...
- [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 ...
- [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 ...
- [Node.js] Level 5. Express
Express Routes Let's create an express route that accepts GET requests on'/tweets' and responds by s ...
- 浅析Node.js的Event Loop
目录 浅析Node.js的Event Loop 引出问题 Node.js的基本架构 Libuv Event Loop Event Loop Phases Overview Poll Phase The ...
- Node.js Event Loop 的理解 Timers,process.nextTick()
写这篇文章的目的是将自己对该文章的理解做一个记录,官方文档链接The Node.js Event Loop, Timers, and process.nextTick() 文章内容可能有错误理解的地方 ...
- Node.js Web 开发框架大全《中间件篇》
这篇文章与大家分享优秀的 Node.js 中间件模块.Node 是一个服务器端 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用程序,编写能够处 ...
- Node.js异步处理CPU密集型任务
Node.js异步处理CPU密集型任务 Node.js擅长数据密集型实时(data-intensive real-time)交互的应用场景.然而数据密集型实时应用程序并非仅仅有I/O密集型任务,当碰到 ...
随机推荐
- [BZOJ5317][JSOI2018]部落战争(闵可夫斯基和)
对于点集$A$,$B$,闵可夫斯基和$C=\{(x1+x2,y1+y2)|(x1,x2)\in A,(y1,y2)\in B\}$.由此可知,对于两个凸包$A$,$B$的闵可夫斯基和$C$满足,$C$ ...
- 问题记录:未设置为接受端口“文件和打印机共享(SMB)”上的连接
解决办法: 网络(右击)——属性——本地连接(右击)——属性——此连接使用下列选项——Microsoft网络的文和打印共享(打上勾)
- 使用ViewPager实现android软件使用向导的功能
现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了. 先看下效果图: 首先需要一个布局文件,是FlameLayout组成的,里面包 ...
- ROS知识(8)----CMakeLists.txt文件编写的理解
ROS(Indigo)编程必须要理解CMakeList.txt的编写规则,教程地址:catkin/CMakeLists.txt,官网有相关的教程,中文的翻译版本写的很不错,教程地址:ROS中的CMak ...
- asp.net 判断日期是否为空
if (Birthday == DateTime.MinValue) { //u can do something here } 首先确保Birthday是不可为null的日期类型.如果可为null就 ...
- python学习笔记5.1-核心类型-集合set类型[转]
转自:http://blog.csdn.net/business122/article/details/7541486 python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系 ...
- PHP通过AJAX及Access-Control-Allow-Origin实现跨域访问
这里的跨域实质上是由浏览器同源策略限制的一类请求场景,浏览器同源策略SOP(Same origin policy)是一种约定,由Netscape公司1995年引入浏览器,它是浏览器最核心也最基本的安全 ...
- Linux命令service - 系统服务管理(转)
用途说明 service命令用于对系统服务进行管理,比如启动(start).停止(stop).重启(restart).查看状态(status)等.相关的命令还包括chkconfig.ntsysv等,c ...
- HDU 4709 Herding (枚举)
Herding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- VPP电源控制(VPP Power)-- 由DC-DC变换集成电路MC34063组成
http://www.willar.com/article/article_view.asp?id=463 由DC-DC变换集成电路MC34063组成,34063 广泛应用于于DC-DC的电源转换电路 ...