Node JS是单线程应用程序,但它通过事件和回调概念,支持并发。 由于Node JS每一个API是异步的,作为一个单独的线程,它使用异步函数调用,以保持并发性。Node JS使用观察者模式。Node线程保持一个事件循环,每当任何任务得到完成,它触发这标志着该事件侦听器函数执行相应的事件。

事件驱动编程

Node.js大量使用事件,这也是为何Node.js是相当快相对于其他类似的技术。当Node启动其服务器,它可以简单地启动它的变量,声明的函数,然后简单地等待发生的事件。

在事件驱动的应用中,通常主循环监听事件,然后触发回调函数时被检测到这些事件之一。

尽管事件似乎类似于回调。不同之处在于如下事实,当异步函数返回其结果的回调函数被调用的地方作为对观察者模式的事件处理。 监听事件的功能作为观察员。每当一个事件被触发,它的监听函数就开始执行。Node.js具有多个内置通过事件模块和用于将事件绑定和事件侦听,如下EventEmitter类可用事件:

// Import events module
var events = require('events');
// Create an eventEmitter object
var eventEmitter = new events.EventEmitter();

以下为事件处理程序绑定使用事件的语法:

// Bind event and even handler as follows
eventEmitter.on('eventName', eventHandler);

我们可以通过编程触发一个事件,如下所示:

// Fire an event
eventEmitter.emit('eventName');

例子

创建一个名为具有以下代码main.js一个js文件:

// Import events module
var events = require('events');
// Create an eventEmitter object
var eventEmitter = new events.EventEmitter();

// Create an event handler as follows
var connectHandler = function connected() {
   console.log('connection succesful.');

   // Fire the data_received event
   eventEmitter.emit('data_received');
}

// Bind the connection event with the handler
eventEmitter.on('connection', connectHandler);

// Bind the data_received event with the anonymous function
eventEmitter.on('data_received', function(){
   console.log('data received succesfully.');
});

// Fire the connection event
eventEmitter.emit('connection');

console.log("Program Ended.");

现在让我们试着运行上面的程序作为检查的输出:

$ mnode main.js

这将产生以下结果:

connection succesful.
data received succesfully.
Program Ended.

Node应用程序是如何工作的?

在Node应用程序,任何异步函数接受回调作为最后的参数和回调函数接受错误的第一个参数。我们再看一下前面的例子了。创建一个名为input.txt的有以下内容的文本文件

Yiibai Point is giving self learning content
to teach the world in simple and easy way!!!!!

创建一个名为具有以下代码的main.js一个js文件:

var fs = require("fs");

fs.readFile('input.txt', function (err, data) {
   if (err){
      console.log(err.stack);
      return;
   }
   console.log(data.toString());
});
console.log("Program Ended");

这里fs.readFile()是一个异步函数,其目的是要读取文件。如果在文件的读出发生了错误,则er对象将包含相应的错误,否则数据将包含该文件的内容。readFile传递err和数据,回调函数后,文件读取操作完成,并最终打印的内容。

Program Ended
Yiibai Point is giving self learning content
to teach the world in simple and easy way!!!!!
 

标签:Node    js    事件    循环

本站文章除注明转载外,均为本站原创或编译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动共创优秀实例教程
转载请注明:文章转载自:易百教程 [http:/www.yiibai.com]
本文标题:Node.js事件循环
本文地址:http://www.yiibai.com/nodejs/nodejs_event_loop.html

Node.js事件循环的更多相关文章

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

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

  2. 6、Node.js 事件循环

    #########################################################################################Node.js 事件循 ...

  3. The Node.js Event Loop, Timers, and process.nextTick() Node.js事件循环,定时器和process.nextTick()

    个人翻译 原文:https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/ The Node.js Event Loop, Ti ...

  4. Node.js 事件循环

    Node.js 是单进程单线程应用程序,但是通过事件和回调支持并发,所以性能非常高. Node.js 的每一个 API 都是异步的,并作为一个独立线程运行,使用异步函数调用,并处理并发. Node.j ...

  5. Node.js 学习(五)Node.js 事件循环

    Node.js 是单进程单线程应用程序,但是通过事件和回调支持并发,所以性能非常高. Node.js 的每一个 API 都是异步的,并作为一个独立线程运行,使用异步函数调用,并处理并发. Node.j ...

  6. [译] 所有你需要知道的关于完全理解 Node.js 事件循环及其度量

    原文地址:All you need to know to really understand the Node.js Event Loop and its Metrics 原文作者:Daniel Kh ...

  7. Node.js 事件循环机制

    Node.js 采用事件驱动和异步 I/O 的方式,实现了一个单线程.高并发的 JavaScript 运行时环境,而单线程就意味着同一时间只能做一件事,那么 Node.js 如何通过单线程来实现高并发 ...

  8. 18.Node.js 事件循环

    转自:http://www.runoob.com/nodejs/nodejs-tutorial.html Node.js 是单进程单线程应用程序,但是通过事件和回调支持并发,所以性能非常高. Node ...

  9. Node.js事件的正确使用方法

    前言 事件驱动的编程变得流行之前,在程序内部进行通信的标准方法非常简单:如果一个组件想要向另外一个发送消息,只是显式地调用了那个组件上的方法.但是在 react 中用的却是事件驱动而不是调用. 事件的 ...

随机推荐

  1. nodejs端口被占用。

    I had the same issue. I ran: $ ps aux | grep node to get the process id, then: $ sudo kill -9 follow ...

  2. SICP 习题 (1.8) 解题总结

    SICP 习题1.8需要我们做的是按照牛顿法求平方根的方法做一个求立方根的过程. 所以说书中讲牛顿法求平方根的内容还是要好好理解,不然后面这几道题做起来就比较困难. 反过来,如果理解了牛顿法求平方根的 ...

  3. [RxJS] Filtering operators: distinct and distinctUntilChanged

    Operator distinct() and its variants are an important type of Filtering operator. This lessons shows ...

  4. [RxJS] Transformation operators: delay and delayWhen

    This lessons teaches about delay and delayWhen: simple operators that time shift. delay(number | dat ...

  5. [AngularJS + Webpack] Production Setup

    Using Angular with webpack makes the production build a breeze. Simply alter your webpack configurat ...

  6. Marquee滚动字幕设置(转)

    <marquee >滚动文字 </marquee> 方向 <direction=#> #=left, right,up,down 方式<bihavior=#& ...

  7. U盘安装centos 6.4教程(总算是弄好了

    参考:http://blog.chinaunix.net/uid-27666459-id-3342477.html http://www.linuxidc.com/Linux/2011-05/3569 ...

  8. android开发之broadcast学习笔记 分类: android 学习笔记 2015-07-19 16:33 32人阅读 评论(0) 收藏

    android中的广播用的太多了,今天稍微总结一下. 按注册方式分为两种: 1.静态注册广播: 静态注册广播就是在androidManifest.xml文件中注册广播,假设我们要实现这样一个效果,在一 ...

  9. Java基础知识强化之集合框架笔记21:数据结构之 数组 和 链表

    1. 数组 2. 链表

  10. htmlentities() 函数

    Definition and Usage定义和用法 The htmlentities() function converts characters to HTML entities.htmlentit ...