1

I'm trying to make a module with an even that I could call from the index file (after a require). My code includes var events = require("events"); and I've wrote only the tricky part here.

index.js:

var reqw = require('./module.js');
reqw.on('data', function(d) {
    console.log(d);
});

module.js:

module.exports = {
    listaccts: function() {
        events.EventEmitter.call(this);
    }
}
util.inherits(exports.listaccts, events.EventEmitter);
exports.listaccts.prototype.listme = function() {
    thisList = this;
    var req = https.request(requestOptions, function(res) {
        res.on('data', function(chuck) {
            store = chuck;
        });
        res.on('end', function(d) {
            thisList.emit("data", store.toString());
        });
    });
}

Searched the whole we and yet to find a proper answer..

Adam Genshaft
  • 756
  • 10
  • 22
  • what do you want to achieve with this? what is your original problem? – eenagy Feb 15 '15 at 13:58
  • I'm trying to get an http request's result up to the index and the only way i could think of is to do it with events. – Adam Genshaft Feb 15 '15 at 14:37
  • I don't understand what you mean. Are you trying to serve content to http requests? – eenagy Feb 15 '15 at 14:58
  • I've edited the code and I hope it's clearer now. I'll try to explain: in the module I have a function that makes an https request. I need to catch the data that the https responds with, and use it in index.js. – Adam Genshaft Feb 15 '15 at 15:24
  • I'm curious, you could have also used the regular callback version or promise-based, or something reactive like that. What made you choose events? – Zlatko Feb 16 '15 at 08:28

1 Answers1

1

Modified your code slightly :

module.js

function listaccts(){

}
util.inherits(listaccts, EventEmitter);
listaccts.prototype.listMe = function(){
    var self = this;
    var store = [];
    console.log('here');
    var req = https.request(requestOptions, function(res) {
        res.on('data', function(chuck) {
            console.log('data');
            store.push(chuck);
        });
        res.on('end', function() {
            console.log('end');
            self.emit("data", store);
        });
    });
    req.end();
};

module.exports = listaccts;

index.js

var reqw = require('./module');
var obj = new reqw();
obj.listMe();
obj.on('data', function(err, data) {
    console.log(err);
});

req.end is important, I have forgot to include and got a never-ending cycle.

Created instance for binding this, so no need for EventEmitter.call. Maybe you want to the listMe function to inside your constructor.

Hope this help.

eenagy
  • 912
  • 1
  • 8
  • 22
  • So first thanks a lot. Second, I need to export more then one function. Any way I can do that? – Adam Genshaft Feb 15 '15 at 18:04
  • I've found it and actually, it's pretty obvious - I made more methods under `listaccts.prototype.[*anotherMethod*]`. So thanks a lot nane! was very helpful :) – Adam Genshaft Feb 18 '15 at 08:17
  • Yes, you found the cleaner way, but you can export multiple functions to check out [module.exports](http://stackoverflow.com/questions/5311334/what-is-the-purpose-of-node-js-module-exports-and-how-do-you-use-it) – eenagy Feb 19 '15 at 15:09