Node.js 질문입니다! API데이터 html호출하기

조회수 947회

안녕하십니까 선배님들 ! 졸업프로젝트를 진행하고 있는데 app.js에서 no1,no2,no3,no4...변수로 가져온 api데이터를 html파일의 Math.round(요기) // data-min= 요기 요기 이 부분에 가져오고 싶은데 어떻게 처리해야 할까요?ㅠㅠ ——————app.js———————- let request = require('request'); let cheerio = require('cheerio');

const $url = 'http://kradata.kra.co.kr:8082/service/api15/getOpenDataList';

const $KEY = '';

const $meet = '1'; const $hr_no = '037703'; const $api_url = $url + '?meet=' + $meet + '&hr_no=' + $hr_no + '&ServiceKey=' + $KEY;

console.log($api_url);

request($api_url, function(err, res, body) { $ = cheerio.load(body);

$('item').each(function(idx){
    let no1 = $(this).find('hrName').text();
    let no2 = $(this).find('winRateT').text();
    let no3 = $(this).find('qnlRateT').text();
    let no4 = $(this).find('winRateY').text();
    let no5 = $(this).find('recentRcDist').text();
    let no6 = $(this).find('chaksunT').text();
    let no7 = $(this).find('chaksunY').text();
    let no8 = $(this).find('chaksun_6').text();

    console.log(`경주마 이름 : ${no1} \n 통산 승률: ${no2}, 통산 복승률: ${no3}, 최근 1년 승률: ${no4} \n 최근경주거리: ${no5}, 통산착순상금: ${no6}, 최근1년착순상금: ${no7}, 최근6개월수득상금: ${no8}`);


});

});

—————————Html——————————-

$(window).load(function() { doughnutWidget.options = { container: $('#container'), width: 100, height: 100, class: 'myClass', cutout: 50 };

doughnutWidget.render(data());

setInterval(init, 2000); });

function init() { doughnutWidget.render(data()); }

function data() { var data = { 통산승률: { val: Math.round(60), color: '#57B4F2', click: function(e) { console.log('hi'); } }, 통산복승률: { val: Math.round(80), color: '#6DED5C' }, 최근1년승률: { val: Math.round(90), color: '#E63329', link: 'http://www.google.com' } };

return data; }

강토마

<div id="container" class="half-left" role="group"></div>


<div class='section half-right'>

    <div class='numscroller-info-top'>최근경주거리</div>
    <div class='numscroller numscroller-big-bottom' data-slno='1' data-min='0' data-max='2000' data-delay='10' data-increment="900">0</div>
</div>
<div class='section half-right'>
    <div class='numscroller-info-top'>통산착순상금</div>
    <div class='numscroller numscroller-big-bottom' data-slno='1' data-min='0' data-max='243900000' data-delay='10' data-increment="1000000">0</div>
</div>
  • (•́ ✖ •̀)
    알 수 없는 사용자

2 답변

  • 요청에 대해 응답을 내려주려면 포트를 열고 listen하여 들어오는 요청을 서빙 해야하는데 위 노드의 로직에는 그러한 것이 전혀 보이지 않습니다. cheerio는 특정 페이지의 크롤링을 위한 것일 뿐, 내가 정작 서빙해야할 리소스들을 다루는 상세 코드는 없네요.

    hapi나 express를 알아보세요. 노드의 웹 서버 프레임워크들입니다.

    설계에 따라서는 view template가 필요할 수 있습니다. handlebars나 pug(jade)로 HTML파일을 재작성해야 할 필요도 있을겁니다.

  • 상대한 답변 대단히 감사드립니다!

    • (•́ ✖ •̀)
      알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)