-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
67 lines (53 loc) · 2.07 KB
/
Copy pathserver.js
File metadata and controls
67 lines (53 loc) · 2.07 KB
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
57
58
59
60
61
62
63
64
65
66
67
var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var app = express();
// set the port of our application
// process.env.PORT lets the port be set by Heroku
var port = process.env.PORT || 8081;
// set the view engine to ejs
//app.set('view engine', 'ejs');
//app.use(express.static(__dirname));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/', function(req, res, next){
url = 'http://www.history.com/this-day-in-history';
request(url, function(error, response, html){
if(!error){
var $ = cheerio.load(html);
var jsonList = { historicalEvent: [] };
$('.categories').filter(function(){
var data = $(this);
data.children().last().children().each(function(i) {
var element = $(this).children();
var title, link, date;
if (element.first().attr('class') == "year"){
date = element.first().text();
title = element.last().text();
link = url + element.last().attr('href');
jsonList.historicalEvent.push({
"date" : date,
"title" : title,
"link" : link
});
}
});
})
}
//fs.writeFile('output.json', JSON.stringify(jsonList, null, 4), function(err){
//
// console.log('File successfully written! - Check your project directory for the output.json file');
//})
res.header('Content-type','application/json');
res.header('Charset','utf8');
res.jsonp(jsonList);
})
})
app.listen(port, function() {
console.log('Our app is running on http://localhost:' + port);
});
exports = module.exports = app;