Complete the tool

In the previous sections, we have already know how to use request to request the API of Youdao Dictionary and use colors to make the tool colorful.

And now we are going to make it complete.

Get parameters

In the first chapter, I’ve introduced commander.js which is created by TJ, however, node-translator does not need lots of parameters, so we could get the parameters directly from the command line.

It is easy for us to get the array of parameters by using process.argv according to the official Node.js documentation.

  1. node test.js a b

The return of process.argv is ['node', 'test.js', 'a', 'b'].

In the previous section, we could get the data needed by GET method. Actually, there are more situations to pass parameters by command line rather than hard coded in our programs.

So we are able to get parameters in the way we mentioned above:

  1. var param = process.argv[2];
  2. var word = param ? param :'';

Then we could append parameters to the URL and request for appropriate return values:

  1. var request = require('request');
  2. request(''+ word,function(error, response, body){
  3. if(!error && response.statusCode ==200){
  4. console.log(body);
  5. }
  6. });