진성욱

비동기를 통한 데이터 처리

var express = require('express');
var router = express.Router();
const Xports = require('../test1.js');
const Zum = require('../test2.js');
const Chosun = require('../test3.js');
const zum = Zum.zum;
const chosun = Chosun.chosun;
exports().then(a => {
console.log(a);
});
const xports = new Array();
router.get('/', function(req, res, next){
res.render('index', {
xports : xports,
zum : zum,
chosun : chosun,
// yna : yna
});
});
console.log("aaaa\n");
for(var i in xports){
console.log("aaaa\n");
console.log(i.datetime);
}
module.exports = router;
var express = require('express');
var router = express.Router();
const Xports = require('../test1.js');
const getXports = require('../test1.js');
const Zum = require('../test2.js');
const Chosun = require('../test3.js');
const Yna = require('../test4.js');
const xports = Xports.xports;
const zum = Zum.zum;
const chosun = Chosun.chosun;
const yna = Yna.yna;
let xports;
(async function() {
try {
xports = await getXports();
console.log(xports);
} catch (e) {
return console.log(e);
}
})();
router.get('/', function(req, res, next){
......@@ -16,11 +23,13 @@ router.get('/', function(req, res, next){
xports : xports,
zum : zum,
chosun : chosun,
yna : yna
// yna : yna
});
});
console.log("aaaa\n");
for(var i in xports){
console.log("aaaa\n");
console.log(i.datetime);
}
module.exports = router;
\ No newline at end of file
module.exports = router;
......
......@@ -13,25 +13,35 @@ const getHtml = async () => {
}
};
getHtml()
.then(html => {
let ulList = [];
//console.log(html.data);
const $ = cheerio.load(html.data);
const $bodyList = $("ul.list_news > li");//.children("");
$bodyList.each(function(i, elem) {
ulList[i] = {
url: 'xportsnews.com' + $(this).find('div.thumb > a').attr('href'),
image_url: $(this).find('div.thumb > a > img').attr('src'),
title: $(this).find('dl.dlist > dt > a').text(),
summary: $(this).find('dd').text().slice(1, -2),
datetime: $(this).find('dd > span.data').text()
};
//console.log(ulList[i]) // list object checking code
});
const data = ulList.filter(n => n.title);
return data;
//return ulList;
}).then(res => console.log(res));
\ No newline at end of file
const getXports = async () => {
return new Promise((resolve, reject) => {
getHtml()
.then(html => {
let ulList = [];
//console.log(html.data);
const $ = cheerio.load(html.data);
const $bodyList = $("ul.list_news > li");//.children("");
$bodyList.each(function(i, elem) {
ulList[i] = {
url: 'xportsnews.com' + $(this).find('div.thumb > a').attr('href'),
image_url: $(this).find('div.thumb > a > img').attr('src'),
title: $(this).find('dl.dlist > dt > a').text(),
summary: $(this).find('dd').text().slice(1, -2),
datetime: $(this).find('dd > span.data').text()
};
//console.log(ulList[i]) // list object checking code
});
const data = ulList.filter(n => n.title);
return data;
//return ulList;
}).then(data => {
resolve(data);
});
});
};
module.exports = getXports;
......
......@@ -31,4 +31,4 @@ getHtml()
const data = ulList.filter(n => n.title);
return data;
//return ulList;
}).then(res => console.log(res));
\ No newline at end of file
});
......
......@@ -31,4 +31,4 @@ getHtml()
const data = ulList.filter(n => n.title);
return data;
//return ulList;
}).then(res => console.log(res));
\ No newline at end of file
});
......
......@@ -69,4 +69,4 @@ app.use('/', indexRouter);
app.listen(PORT, function(){
console.log('Code run in https://localhost:'+PORT);
});
*/
\ No newline at end of file
*/
......
......@@ -10,7 +10,7 @@
<div class = 'main'>
<div class = 'center'>
<div>
<% for(var i in xports) { %>
<% for(var i of xports) { %>
<li>
<div class='item'>
<div class='info'>
......@@ -36,4 +36,4 @@
</div>
</div>
</body>
</html>
\ No newline at end of file
</html>
......