-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
56 lines (47 loc) · 1.25 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
var cheerio = require('cheerio')
, superagent = require('superagent')
, express = require('express')
, async = require('async')
, url = require('url');
var cnodeUrl = 'http://cnodejs.org/';
function analyze(page) {
var $ = cheerio.load(page);
return ({
title: $('.topic_full_title').text().trim(),
comment1: $('.reply_content').eq(0).text().trim(),
});
}
function fetchUrl(url, callback) {
superagent.get(url)
.end(function (err, res) {
var page = res.text;
var postTime = analyze(page);
callback(null, postTime);
});
}
// start
var app = express();
app.get('/', function (req, res, next) {
superagent.get(cnodeUrl)
.end(function(err,sres){
if(err){
return console.error(err);
}
var topicUrls = [];
var $ = cheerio.load(sres.text);
$('#topic_list .topic_title').each(function(idx,element){
var $element = $(element);
var href = url.resolve(cnodeUrl,$element.attr('href'));
topicUrls.push(href);
})
async.mapLimit(topicUrls, 5, function(url, callback) {
fetchUrl(url, callback);
}, function (err, result) {
res.send(result);
});
});
});
// listen
app.listen(3000, function () {
console.log('app is listening at port 3000');
});