Created
April 6, 2016 15:23
-
-
Save diegoachury/cabe690cbb0ca1bd01516db14eb9a883 to your computer and use it in GitHub Desktop.
servidor express scrap.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_http_outgoing.js:335 | |
throw new Error('Can\'t set headers after they are sent.'); | |
^ | |
Error: Can't set headers after they are sent. | |
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11) | |
at ServerResponse.header (/Users/diegoachury/node-projects/newspapers-huila/node_modules/express/lib/response.js:718:10) | |
at ServerResponse.send (/Users/diegoachury/node-projects/newspapers-huila/node_modules/express/lib/response.js:163:12) | |
at done (/Users/diegoachury/node-projects/newspapers-huila/node_modules/express/lib/response.js:957:10) | |
at Immediate._onImmediate (/Users/diegoachury/node-projects/newspapers-huila/node_modules/express-handlebars/lib/utils.js:26:13) | |
at processImmediate [as _immediateCallback] (timers.js:383:17) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var express = require("express"), | |
exphbs = require('express-handlebars'), | |
request = require("request"), | |
cheerio = require("cheerio"), | |
app = express(), | |
port = process.env.PORT || 3002; | |
//array | |
var urlsArray = ['http://www.lanacion.com.co/', 'http://www.diariodelhuila.com/']; | |
// todos | |
app.get('/', function (req, res) { | |
urlsArray.forEach(function (item, index, urlsArray) { | |
console.log(item, index); | |
request(item, function (err, resp, body) { | |
if (!err && resp.statusCode === 200) { | |
var $ = cheerio.load(body), | |
pageURLs = []; | |
if(index === 0 ){ | |
console.log("haciendo request: " + item ); | |
$('a.moduleItemTitle').map(function (i, links) { | |
var articleText = $(links).text(), | |
articleLink = $(links).attr('href'); | |
pageURLs.push({ | |
link: articleLink, | |
desc: articleText | |
}); | |
}); | |
var dataNacion = JSON.parse(JSON.stringify(pageURLs)); | |
console.log("el valor dentro del IF " + JSON.stringify(pageURLs) ); | |
res.render('two', {dataNacion}); | |
}// cierra if nacion | |
if (index === 1 ){ | |
console.log("haciendo request: " + item ); | |
$('article header h1 a').map(function (i, links) { | |
var articleText = $(links).text(), | |
articleLink = $(links).attr('href'); | |
pageURLs.push({ | |
link: articleLink, | |
desc: articleText | |
}); | |
}); | |
var dataDiario = JSON.parse(JSON.stringify(pageURLs)); | |
console.log("el valor dentro del IF " + JSON.stringify(pageURLs) ); | |
res.render('two', {dataDiario}); | |
} | |
}//cierra desicion | |
}); //cierra request | |
}); //cierra foreach | |
});// cierra url get / | |
// manejador de plantilla con handlebar | |
app.engine('handlebars', exphbs({defaultLayout: 'main'})); | |
app.set('view engine', 'handlebars'); | |
app.listen(port); | |
console.log('Your server goes on localhost:' + port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment