Input-reading library for Node.JS Recently I have been busy with nodejs and I thought to create something like this Java input library, so I started with some rough code like below:

'use strict';
const readline = require('readline');
const fs = require('fs');

class StdIn {
  constructor(opt_name) {
    this.name = opt_name;
    this.rs = this.name ? fs.createReadStream(this.name) : process.stdin
  }

  readInt(callback) {
    this.rs.on('data', (chunk) =&gt; {
      let line = chunk.toString();
      let numbers = line.trim().split(/\s/);
      numbers.forEach(num =&gt; {
        callback.call(this, num);
      });
    });
    this.rs.on('end', () =&gt; {
      process.exit(0);
    });
  }

  readLine(callback) {
    this.rs.on('data', (chunk) =&gt; {
      let line = chunk.toString();
      callback.call(this, line);
    });
    this.rs.on('end', () =&gt; {
      process.exit(0);
    });
  }
}

const stdIn = new StdIn(process.argv[2]);

//stdIn.readInt(num =&gt; {
//  console.log(num);
//});

stdIn.readLine(line =&gt; {
  console.log(line);
});


Its working fine till now and buts its still incomplete and the code is too messy, the problem clearly visible here that I am trying to push event oriented code into my object oriented world, I am confused basically for two things:


It is practical to do so?
If yes then what would be the best way?
... the problem clearly visible here that I am trying to push event oriented code into my object oriented world


There is nothing wrong with object oriented and event oriented code being intermingled. It's like saying "toast and jam should not be used in the same breakfast." Not only can you mix the two, depending on your needs you should mix the two. The "problem" I think you're referring to is mixing object oriented with functional programming.

Since this is JavaScript, and it handles both programming paradigms good enough, feel free to mix the two. The only thing to consider is "callback Hell" where you have callbacks nested inside callbacks. For this, the Promise Pattern was created.

On to my favorite subject, naming things. 


In the StdIn constructor, the argument is called opt_name, yet everything else appears to be written in camelCase. Pick either snake_case or camelCase and be consistent. To be consistent with other API's I would recommend camelCase
The opt_name argument could just be called name since there is not ambiguous reference between the argument and the property on the StdIn object
Rename the rs property to something meaningful, like:


readStream
input
inputStream
stream



Otherwise I don't see any glaring issues.