예전에 공공데이터 관련해서 크롤링해야흔 부분이 있었는데 node.js로 바로 크롤링한 데이터를 react로 전송했던 부분이 있어 코드를 공유하고자한다.
const getBreeds = async () => {
var url = 'http://openapi.data.go.kr/openapi/service/rest/Covid19/getCovid19InfStateJson';
var queryParams = '?' + encodeURIComponent('ServiceKey') + '=zWJGWsZTX%2FrJP6gryI%2FZZCnS23KP%2FaPF7IzFwYJlTUCKgq2AG3zkalo64Vc5Suf%2FUHoKciZw0dByIBDcP1WR%2Fg%3D%3D'; /* Service Key*/
queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('1'); /* */
queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('10'); /* */
queryParams += '&' + encodeURIComponent('startCreateDt') + '=' + encodeURIComponent('20200310'); /* */
queryParams += '&' + encodeURIComponent('endCreateDt') + '=' + encodeURIComponent('20200315'); /* */
queryParams += '&' + encodeURIComponent('SEQ') + '=' + encodeURIComponent('30'); /* */
request({
url: url+queryParams,
method: 'GET'
}, function (error, response, body) {
//console.log('Status', response.statusCode);
//console.log('Headers', JSON.stringify(response.headers));
var result = body
var xmlToJson = convert.xml2json(result, {compact: true, spaces: 4});
var Json_result = xmlToJson
//console.log(`xml to json => ${xmlToJson}`)
console.log(`xml to json => ${xmlToJson.body}`)
const json = JSON.parse(parser.toJson(result))
const item = json.response.body.items.item[0]
console.log(item)
// console.table(item)
// const addrs = item.map(i => i.addr)
// const template = `l
// each addr in addrs
// li= addr
// `
// const html = pug.render(template, { addrs: addrs })
// console.log(html)
});
};
router.get('/data', function (req, res, next) {
getBreeds()
});
module.exports = router;