Tag: 事件

直接在可写stream中pipe道可读stream

有什么区别: const stream = createReadStream(source).pipe(createWriteStream(destination)); stream.on("error", (error) => { stream.destroy(); stream.removeListener("close"); reject(error); }); stream.on("close", () => { // do something }); VS: const writable = createWriteStream(destination); const readable = createReadStream(source); readable.on("error", onError); writable.on("error", onError); function onError(error) { readable.destroy(); writable.destroy(); writable.removeListener("close", onClose); reject(error); } 为什么我们应该手动销毁stream如果发生错误? 节点不会自动做到这一点? 谢谢。

NodeJS等待callback完成事件发射

我有和应用程序写在NodeJS与Express,并试图使用EventEmitter来创build一种插件体系结构,通过侦听发出的事件插入到主代码的插件。 我的问题是当一个插件函数发出一个asynchronous请求(在这种情况下从mongo获取数据)时,这会导致插件代码完成并返回到原始发射器,然后在插件代码中的asynchronous请求之前完成执行饰面。 例如: 主应用程序: // We want to modify the request object in the plugin self.emit('plugin-listener', request); 插入: // Plugin function listening to 'plugin-listener', 'request' is an arg console.log(request); // Call to DB (async) this.getFromMongo(some_data, function(response){ // this may not get called until the plugin function has finished! } 我从“getFromMongo”函数回避主要代码的callback函数的原因是可能有0个或多个插件监听事件。 理想情况下,我想要一些方法来等待数据库的东西完成,然后返回到主应用程序的控制 非常感谢

执行'n'asynchronouscallback后,node.js执行函数

我不能得到如何执行一个callback' n 'asynchronousfunction执行后,例如: var after4AsyncOperation = function(){ //do something… } process.nextTick(function(){ //do stuff }) process.nextTick(function(){ //do stuff }) process.nextTick(function(){ //do stuff }) process.nextTick(function(){ //do stuff }) 有没有一种方法来执行after4AsyncOperation之后的4个asynchronous函数,而不必编写一个function之一,例如: var after4AsyncOperation = function(){ //do something… } process.nextTick(function(){ //do stuff process.nextTick(function(){ //do stuff process.nextTick(function(){ //do stuff process.nextTick(function(){ //do stuff after4Asyncoperation(); }) }) }) })

在NodeJS中发送HTTP响应之前等待事件发生?

我正在寻找一个解决scheme,在发送HTTP响应之前等待事件发生。 用例 这个想法是我在我的一个路由中调用一个函数: zwave.connect("/dev/ttyACM5"); 这个函数立即返回。 但是有两个事件会告诉你连接设备是成功还是失败: zwave.on('driver ready', function(){…}); zwave.on('driver failed', function(){…}); 在我的路由中,我想知道在发送HTTP响应之前,设备是成功还是失败。 我的“解决scheme” 当事件发生时,我将事件保存在数据库中: zwave.on('driver ready', function(){ //In the database, save the fact the event happened, here it's event "CONNECTED" }); 在我的路线中,执行connect函数并等待事件出现在数据库中: router.get('/', function(request, response, next) { zwave.connect("/dev/ttyACM5"); waitForEvent("CONNECTED", 5, null, function(){ response.redirect(/connected); }); }); // The function use to wait for the event […]

节点的JS – 如何告诉如果一个套接字已经打开与Einaros WS套接字模块?

我目前使用einaros的NodeJS WS模块来创build一个websocket客户端。 在初始化套接字客户端时,我使用“open”事件来确定连接何时打开,但一旦打开,我将如何检查它是否仍然打开? 在“WebSocket”对象上是否有一个属性可以用来快速检查套接字的状态? 是 – websocketInstance.readyState – 是否足够?

投放事件不在主干视图上触发

当这个#〜@@!我很高兴编码和学习骨干 发生了! 我使用require来将我的观点从模型中分离出来 在我的Backbone视图中,我处理这个事件: define(['…'],function(…) { var DishView = Backbone.View.extend({ template: _.template(dish), tagName: 'div', id: 'dish', initialize: function() { console.log('initializing dishView'); this.model.on('change', this.render, this); }, render: function(){ console.log('rendering dishView'); this.$el.html(this.template(this.model.toJSON())); return this; }, events: { 'click #relations-menu .newItem': 'launch_modal_relations', 'click #delete' : 'delete_dish', 'click #save-basic-changes': 'save_basic', 'drop #dropPicture' : 'dropHandler', 'dragenter #dropPicture' : 'alertMe' }, […]

“请求”和“回应”从哪里来,我怎么知道的?

我已经决定学习节点,所以我遵循,首先, 节点初学者书 。 正如我猜想其他很多资源,有“简单的HTTP服务器”,第一步,像这样: var http = require("http"); http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); response.end(); }).listen(8888); 据我了解,当有人,在这种情况下,我虽然localhost:8888 ,发出一个请求,一个事件被触发,并传递给http.createServer的匿名函数被激发。 我把这些文档放到了我发现有用的http.createserver的文档中: http.createServer([requestListener]) 返回一个新的Web服务器对象。 requestListener是一个自动添加到“请求”事件的函数。 (来自node.js站点) 我无法find或弄清楚这是如何触发函数得到它的parameter passing,以及如何find它。 所以…我怎么知道这些参数来自哪里,他们提供什么方法​​等等? 提前致谢!

Node.js EventEmitter:如何将类上下文绑定到事件侦听器,然后删除此侦听器

有没有办法访问事件监听器方法中的类上下文有可能删除监听器? 例1: import {EventEmitter} from "events"; export default class EventsExample1 { private emitter: EventEmitter; constructor(private text: string) { this.emitter = new EventEmitter(); this.emitter.addListener("test", this.handleTestEvent); this.emitter.emit("test"); } public dispose() { this.emitter.removeListener("test", this.handleTestEvent); } private handleTestEvent() { console.log(this.text); } } 在这个例子中,删除侦听器的工作,但handleTestEvent()方法没有使用this类上下文的访问。 this指向EventEmitter上下文,所以this.text不可访问。 例2: import {EventEmitter} from "events"; export default class EventsExample2 { private emitter: EventEmitter; constructor(private text: […]

创build自定义EventEmitter时是否需要events.EventEmitter.call(this)?

在自定义事件发射器构造函数中有很多不使用events.EventEmitter.call(this)的例子,而其他的则使用它( 官方文档 ): var events = require('events') , util = require('util'); var MyEmitter = function () { events.EventEmitter.call(this); }; util.inherits(MyEmitter, events.EventEmitter); MyEmitter.prototype.write = function () { this.emit('tick'); }; 随着我对JavaScript的基本理解,我不知道是否需要它。 是否需要调用EventEmitter初始化的东西?

从标准input读取时,不能使用CTRL D触发'结束'事件

在下面的代码中 process.stdin.resume(); process.stdin.setEncoding('utf8'); process.stdin.on('data', function(chunk) { process.stdout.write('data: ' + chunk); }); process.stdin.on('end', function() { process.stdout.write('end'); }); 我不能用ctrl + D触发'end'事件,而ctrl + C只是退出而不触发它。 hello data: hello data data: data foo data: foo ^F data: ♠ ^N data: ♫ ^D data: ♦ ^D^D data: ♦♦