[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密集型任务,当碰到 ...
随机推荐
- luoguP4715 [英语]Z语言 平衡树+hash
显然只能有$hash$来做.... 我们需要一个东西来维护$\sum i * seed^{rank[i]}$ 很自然地联想到平衡树 如果以序列下标建立一棵平衡树,那么无法处理 因此,可以以权值为下标建 ...
- Java并发(二十一):线程池实现原理
一.总览 线程池类ThreadPoolExecutor的相关类需要先了解: (图片来自:https://javadoop.com/post/java-thread-pool#%E6%80%BB%E8% ...
- BZOJ 4554: [Tjoi2016&Heoi2016]游戏 二分图匹配
4554: [Tjoi2016&Heoi2016]游戏 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4554 Descripti ...
- MVC基类控制器的会话丢失重新登录权限过滤
SSOController.cs请查看以下代码: public class SSOController : Controller { public const string Token = " ...
- OPENCV----在APP性能测试中的应用(一)
应用项目: APP的性能测试 应用场景: APP启动速度 视频开播速度 加载速度 等~~ 缘来: 基于APP日志和UiAutomator的测试方案,测试结果不能直白且精确的反应,用户的体验 ...
- MySQL在控制台上以竖行显示表格数据
直接在SQL语句后面加\G即可,如: select * from user limit 10\G; 如果想要知道这些参数可以直接在命令行后面加入\?
- Windows界面编程第四篇 异形窗体 高富帅版 ---博客
http://blog.csdn.net/morewindows/article/details/8451638
- JQUERY AJAX无刷新异步上传文件
AJAX无刷新上传文件并显示 http://blog.csdn.net/gao3705512/article/details/9330637?utm_source=tuicool jQuery For ...
- MVC文件上传07-使用客户端jQuery-File-Upload插件和服务端Backload组件裁剪上传图片
本篇通过在配置文件中设置,对上传图片修剪后保存到指定文件夹. 相关兄弟篇: MVC文件上传01-使用jquery异步上传并客户端验证类型和大小 MVC文件上传02-使用HttpPostedFileB ...
- Tomcat与Gzip与缓存
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...