Yuseunggeun

접속마다 api 접속하도록 변경

This diff is collapsed. Click to expand it.
......@@ -92,6 +92,10 @@ router.get('/',function(req,res){
res.render('main',{});//메인 페이지
});
router.get('/rank',function(req,res){
console.log('Rank page');
res.render('rank',{ //전체순위(가성비)
......@@ -168,25 +172,41 @@ router.post('/search',function(req,res,next){
itemcount: Searchcount
});
}, 1000);
});
router.post('/shop',function(req,res,next){
var keyword = req.body.word;
req.query.query = keyword;
var api_url = 'https://openapi.naver.com/v1/search/shop?query=' + encodeURI(req.query.query); // json 결과
// var api_url = 'https://openapi.naver.com/v1/search/shop.xml?query=' + encodeURI(req.query.query); // xml 결과
var options = {
url: api_url,
headers: {'X-Naver-Client-Id':client_id, 'X-Naver-Client-Secret': client_secret}
};
request.get(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
//res.writeHead(200, {'Content-Type': 'text/json;charset=utf-8'});
var searchitem = new Array();
var shopjson = JSON.parse(body);
//console.log(shopjson['items']);
//console.log(typeof(body));
//console.log(body);
searchitem = shopjson['items'];
res.render('search',{
itemlist: searchitem,
itemname: keyword
});
} else {
res.status(response.statusCode).end();
console.log('error = ' + response.statusCode);
}
});
});
/*
function searching(name){
var api_url = 'https://openapi.naver.com/v1/search/shop?query=' + encodeURI(name); // json 결과
......@@ -236,6 +256,8 @@ async function createDB(){
createDB();
/*
router.post('/shop', function(req, res) {
var keyword = req.body.word;
req.query.query = keyword;
......
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css">
<link href="https://fonts.googleapis.com/css2?family=Nanum+Gothic&display=swap" rel="stylesheet">
<style>
body{
font-family: 'Nanum Gothic', sans-serif;
background-color: rgb(219, 242, 243);
color:black;
margin: 0px;
padding: 0px;
}
table{
font-family: 'Nanum Gothic', sans-serif;
font-size: 20px;
padding: 0px;
margin: 0 auto;
text-align: center;
color: black;
line-height: 1;
border-style: outset;
}
td{
margin: 0px;
padding: 0px;
width: 230px;
border-style: solid;
}
a, a:visited{
color:blue;
text-decoration: none;
}
input[type=submit] {
font-size: 20px;
width: 120px;
height: 60px;
padding: 12px 20px;
margin: 0 auto;
box-sizing: border-box;
text-align: center;
}
#num{
width: 50px;
}
.box{
height:200px;background-color: skyblue; position:relative;
}
h1{
font-size: 60px;
text-align: center;
color: black;
}
h2{
text-align: center;
margin:0px;
}
img{
height:130px;
margin: 0px;
}
</style>
</head>
<body>
<h1>
검색 결과
</h1>
<center>
<form method="GET" action="http://localhost:3000/">
<input type="submit" id="button" value="돌아가기">
</form>
</center>
<% for(var i = 0;i < 10 ;i++){ %>
<table>
<tr>
<td id='num'><h2> <%= (i+1) %> </h2></td>
<td><h2> <%= itemname %> </h2></td>
<td><h2> <img src = <%= itemlist[i]['image'] %>></h2> </td>
<td><h2> 최저가 : <%= itemlist[i]['lprice'] %>원</h2> </td>
<td><h2> 최고가 : <%= itemlist[i]['hprice'] %>원</h2> </td>
<td><h2> <a href = <%=itemlist[i]['link']%> target=_blank>구매링크</a></h2></td>
</tr>
</table>
<%}%>
</body>
</html>
\ No newline at end of file