import * as request from 'request';

const url = 'https://api.exchangeratesapi.io/latest';
let result = '';
request.get(url)
    .on('response', res => {
      console.log(`statusCode = ${res.statusCode}`);  
    })
    .on('data', data => {
        console.log('ได้ข้อมูล');
      result += data;
    })
    .on('end', res => {
      //console.log(`พร้อมทำงาน เอา data ไปทำอะไรดี`);  
      const exchange = JSON.parse(result);
      //console.log(exchange);
      const r = exchange.rates;
      let money = 1000;
      console.log(`เงิน ${money} บาท = ${money*r.USD/r.THB} USD`)
    })