var http = require('http');
//var dataQueryUrl = `http://api.avatardata.cn/ChengYu/Search?key=${apikey}&keyWord=`;

function request(key, word, cb) {
  var options = {
    host: 'http://api.avatardata.cn',
    path: `/ChengYu/Search?key=${key}&keyWord=${word}`,
    port: '80',
    method: 'GET'
  };

  var callback = function(response) {
    var str = ''
    response.on('data', function (chunk) {
      str += chunk;
    });

    response.on('end', function () {
      cb(str);
    });
  }

  var req = http.request(options, callback);
  //This is the data we are posting, it needs to be a string or a buffer
  // req.write(JSON.stringify({
  //
  // }));
  req.end();
}

module.exports = request;
var http = require('http');
//var dataQueryUrl = `http://api.avatardata.cn/ChengYu/Search?key=${apikey}&keyWord=`;

function request(key, word, cb) {
  var options = {
    host: 'http://api.avatardata.cn',
    path: `/ChengYu/Search?key=${key}&keyWord=${word}`,
    port: '80',
    method: 'GET'
  };

  var callback = function(err, response) {
    if (err) {
      throw err;
    }

    var str = ''
    response.on('data', function (chunk) {
      str += chunk;
    });

    response.on('end', function () {
      console.log(str);
      cb(str);
    });
  }

  var req = http.request(options, callback);
  //This is the data we are posting, it needs to be a string or a buffer
  // req.write(JSON.stringify({
  //
  // }));
  req.end();
}

module.exports = request;
add error handler