[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 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的更多相关文章
- 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密集型任务,当碰到 ...
随机推荐
- BZOJ.2916.[POI1997]Monochromatic Triangles(三元环)
题目链接 \(Description\) n个点的完全图,其中有m条边用红边相连,其余边为蓝色.求其中三边同色的三角形个数. \(Solution\) 直接求同色 除了n^3 不会.. 三角形总数是C ...
- 【洛谷】2607: [ZJOI2008]骑士【树形DP】【基环树】
P2607 [ZJOI2008]骑士 题目描述 Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬. 最近发生了一件可怕的事情,邪恶的Y国发动了一 ...
- POJ 1654 Area 计算几何
#include<stdio.h> #include<string.h> #include<iostream> #include<math.h> usi ...
- vue父子组件使用时遇到的一个问题
子组件一定要写在父组件之前,例如: //子vue,这里遇到一个坑,那就是子vue一定要写在父vue前面,不然会报错. Vue.component('todo-item', { template: $( ...
- KVM资源划分分配技巧
kvm有个叫做超分的概念,根据这个特性可以分配出超出物理机配置的数台虚拟机. 以下是自己总结的一些划分技巧: 一.最保守方法(性能最好) 根据物理机的资源,按虚拟机的数量叠加但不超过物理机的总和.不超 ...
- Windows Azure 系列-- Azure Queue的操作
- Storage Account. 和之前介绍的Azure Table和AzureBlob一样.你须要一个StorageAccount,仅仅须要创建1次AzureStorageAccount就好了, ...
- MVC打印表格,把表格内容放到部分视图打印
假设在一个页面上有众多内容,而我们只想把该页面上的表格内容打印出来,window.print()方法会把整个页面的内容打印出来,如何做到只打印表格内容呢? 既然window.print()只会打印整页 ...
- spring mvc 下 applicationContext 和webApplicationContext
spring中的ApplicationContexts可以被限制在不同的作用域.在web框架中,每个DispatcherServlet有它自己的WebApplicationContext,它包含了Di ...
- CMMI管理体系
帮助企业对软件工程过程进行管理和改进,增强开发与改进能力,从而按时,不超过预算地开发软件. CMMI为改进一个组织的各种过程提供了一个单一的集成化框架,新的集成模块框架消除了各个模型的不一致性,减少了 ...
- freecms怎样在信息列表类标签中提取info.content
原文地址:http://javaz.cn/site/javaz/site_study/info/2015/22026.html 项目地址:http://www.freeteam.cn/ 因为info. ...