Search code examples
node.jsreadline

Nodejs how to create custom method for readline interface


How to create a custom method for readline interface ?

myin = {}

    myin.on = function (a,b) {

    }

    myin.resume = function () {

    }



var rl = readline.createInterface({
      input: myin
  });


 rl.on('line', function (data) {
     console.log(data); 
 });

I want to create my input method for using in readline like when call myin.on("data","aaaaa") ... myin.on("data","bbb\n") readline grab the line ..

but I can't find any source or doc ..


Solution

  • I found that !

    myin = {}
    
        myin.on = function (a,b) {
                          if (a == 'data') {
    
                           myin.ondata= b;    
    
                     }
        }
    
        myin.resume = function () {
    
        }
    

    and then call like this :

     myin.ondata("bbbb");
     myin.ondata("aaaaa\n");
    

    and output of rl.on('line' is like this : bbbbaaaaa