YS's develop story

Node Js, OpenAPI๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ DB์— ์ €์žฅํ•˜๊ธฐ (Request.js) ๋ณธ๋ฌธ

๊ธฐํƒ€

Node Js, OpenAPI๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ DB์— ์ €์žฅํ•˜๊ธฐ (Request.js)

Yusang 2021. 6. 25. 11:06

๐Ÿ‘จ‍๐Ÿ’ป OpenAPI๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ DB์— ์ €์žฅํ•˜๊ธฐ (Request.js)

โŒ› Problem

 

๊ณต๊ณต๋ฐ์ดํ„ฐ ํฌํ„ธ

๊ตญ๊ฐ€์—์„œ ๋ณด์œ ํ•˜๊ณ  ์žˆ๋Š” ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ๋ฅผใ€Ž๊ณต๊ณต๋ฐ์ดํ„ฐ์˜ ์ œ๊ณต ๋ฐ ์ด์šฉ ํ™œ์„ฑํ™”์— ๊ด€ํ•œ ๋ฒ•๋ฅ (์ œ11956ํ˜ธ)ใ€์— ๋”ฐ๋ผ ๊ฐœ๋ฐฉํ•˜์—ฌ ๊ตญ๋ฏผ๋“ค์ด ๋ณด๋‹ค ์‰ฝ๊ณ  ์šฉ์ดํ•˜๊ฒŒ ๊ณต์œ •ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ณต๊ณต๋ฐ์ดํ„ฐ(Datase

www.data.go.kr

์œ„ ์‚ฌ์ดํŠธ์— ๋“ฑ๋ก๋œ ๋‹ค์–‘ํ•œ ๊ณต๊ณต๋ฐ์ดํ„ฐ๋“ค์„ OpenAPI๋ฅผ ํ†ตํ•ด ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ €๋Š” ๊ณต๊ณต๋ฐ์ดํ„ฐ ํฌํ„ธ์— ๋“ฑ๋ก๋œ ์„œ์šธ์‹œ์˜ ๋ชจ๋“  ๋ณ‘์› ์ •๋ณด๊ฐ€ ํ•„์š”ํ–ˆ๊ณ 

์ด๋ฅผ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์ดํŠธ์—์„œ ๋ณ‘์›์ •๋ณด OpenAPI key๋ฅผ ๋ฐœ๊ธ‰๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. 

 

๋ฐœ๊ธ‰๋ฐ›์€ Service key๋ฅผ ํ†ตํ•ด Postman๊ณผ ๋ธŒ๋ผ์šฐ์ €๋กœ ๊ฐ๊ฐ ์š”์ฒญํ•œ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ์ˆ˜๋ฐฑ ๊ฐœ์˜ ๋ณ‘์›์ •๋ณด๋ฅผ ํ•œ๋ˆˆ์— ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋ ‡๋‹ค๋ฉด ์ด๋ ‡๊ฒŒ ๋งŽ์€ ๋ณ‘์› ์ •๋ณด๋“ค์„ ๋‚˜์˜ DB์— ์ €์žฅํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”?

์ด๋ฅผ ์ผ์ผ์ด DB์— ์ž…๋ ฅํ•˜๋Š” ๊ฑด ๋ง๋„ ์•ˆ ๋œ๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š”๋ฐ์š”

์ €๋Š” ์ด๋ฅผ Node Js์—์„œ request๋ชจ๋“ˆ์„ ํ™œ์šฉํ•˜์—ฌ DB์— ํ•œ ๋ฒˆ์— ์ €์žฅํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

๐Ÿ“œ Solution

 

Postman | The Collaboration Platform for API Development

Postman makes API development easy. Our platform offers the tools to simplify each step of the API building process and streamlines collaboration so you can create better APIs faster.

www.postman.com

Postman์—์„œ ๋‚ด๊ฐ€ ๋ฐœ๊ธ‰๋ฐ›์€ ServiceKey๋ฅผ ํ†ตํ•ด OpenAPI๋ฅผ ์š”์ฒญํ•ด ์ค์‹œ๋‹ค.

 

 

OpenAPI๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ์˜ค๋ฅธ์ชฝ ์ƒ๋‹จ์— ์œ„์˜ ์‚ฌ์ง„๊ณผ ๊ฐ™์€ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ด ์ค๋‹ˆ๋‹ค.

 

 

์œ„ ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•˜์—ฌ ๋‚ด ํ”„๋กœ์ ํŠธ์— ๋ถ™์—ฌ ๋„ฃ๊ธฐ ํ•ด ์ค์‹œ๋‹ค.

 

 

npm i request

request ๋ชจ๋“ˆ์„ ์„ค์น˜ํ•ด ์ค๋‹ˆ๋‹ค.

 

 

    const numOfRows = 10;
    const ServiceKey = '';
    const hospitalCategory = 80;
    //14ํ”ผ๋ถ€๊ณผ 61์น˜๊ณผ 08์„ฑํ˜•์™ธ๊ณผ 12์•ˆ๊ณผ 01๋‚ด๊ณผ 05์ •ํ˜•์™ธ๊ณผ 80ํ•œ๋ฐฉ

    let request = require('request');
    let options = {
        'method': 'GET',
        'url': 'http://apis.data.go.kr/B551182/hospInfoService1/getHospBasisList1?numOfRows=' +
                numOfRows + '&_type=json&sidoCd=110000&clCd=21&ServiceKey=' +
                ServiceKey + '&dgsbjtCd=' + hospitalCategory,
        'headers': {
            'Cookie': 'WMONID=sN2sN5XVW3y'
        }
    };

    request(options, function (error, response, body) {
        if (error) {
            throw new Error(error);
        }
        let info = JSON.parse(body);

        for (i in info['response']['body']['items']['item']) {
            console.log('๋ณ‘์›์ด๋ฆ„ : ' + info['response']['body']['items']['item'][i]['yadmNm']);
            console.log('๋ณ‘์›์ฃผ์†Œ : ' + info['response']['body']['items']['item'][i]['addr']);
            console.log(
                '๋ณ‘์›์ „ํ™”๋ฒˆํ˜ธ : ' + info['response']['body']['items']['item'][i]['telno']
            );
            console.log('๋ณ‘์›์œ„๋„ : ' + info['response']['body']['items']['item'][i]['YPos']);
            console.log('๋ณ‘์›๊ฒฝ๋„ : ' + info['response']['body']['items']['item'][i]['XPos']);
            console.log(" ")
        }
    });

์ œ๊ฐ€ DB์— ์ €์žฅํ•  ์ •๋ณด๋Š” ๋ณ‘์› ์ด๋ฆ„, ๋ณ‘์› ์œ„์น˜(์œ„๋„, ๊ฒฝ๋„), ๋ณ‘์› ์ฃผ์†Œ, ๋ณ‘์› ์ „ํ™”๋ฒˆํ˜ธ

์ด๋ ‡๊ฒŒ 5๊ฐœ์ด๊ธฐ ๋•Œ๋ฌธ์— ์œ„์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

 

 

์ด๋Š” ์‹คํ–‰ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค. 
console.log๋กœ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋“ค์„ ๋ชจ๋‘ ๋ณ€์ˆ˜์— ํ• ๋‹น์— ์ฃผ์–ด์„œ ๋ถˆ๋Ÿฌ์˜จ ๋ณ‘์›์˜ ์ •๋ณด๋งŒํผ

for๋ฌธ์„ ํ™œ์šฉํ•˜์—ฌ Insert ์ฟผ๋ฆฌ๋ฌธ์œผ๋กœ DB์— ๋„ฃ์–ด์ฃผ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!

Comments