request.js 852 Bytes
var request = require('request');
const convert = require("xml-js")

var url = 'http://apis.data.go.kr/6410000/busarrivalservice/getBusArrivalList';
var queryParams = '?' + encodeURIComponent('serviceKey') + process.env.SERVICE_KEY; /* Service Key*/
queryParams += '&' + encodeURIComponent('stationId') + '=' + encodeURIComponent('228000710'); /* */

function callRequest() {
    return new Promise((resolve, reject) => {
        request({
            url: url + queryParams,
            method: 'GET'
        }, function (error, response, body) {
            const data = convert.xml2js(body, { compact: true }).response.msgBody
            if (data)
                resolve(JSON.stringify(data.busArrivalList[1]));
            else
                resolve("운행 중인 버스가 없습니다.");
        });
     })
}

module.exports = callRequest;