Last active
April 22, 2016 04:44
-
-
Save rootasjey/ce68cb4cc0098a8a1f7b649bb8d833d1 to your computer and use it in GitHub Desktop.
jsdom html parsing from server-side
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
// Get the last articles from the blog | |
.get('/getblog', function (req, res) { | |
var jqueryPath = __dirname + "/public/js/jquery-2.1.1.min.js"; | |
jsdom.env( | |
"http://rootasjey.github.io/", | |
[jqueryPath], | |
function (errors, window) { | |
// Jquery object | |
var $ = window.$; | |
// Posts titles array | |
var postTitles = $(".post .post-title a"); | |
// Posts summaries array | |
var postSummaries = $(".post .post-excerpt p"); | |
// Post links array | |
var postLinks = $(".post .post-excerpt p .read-more"); | |
// json array to return | |
var jsonArray = []; | |
for (var i = 0; i < postTitles.length; i++) { | |
var title, summary, link; | |
title = $(postTitles[i]).text(); | |
summary = $(postSummaries[i]).text(); | |
link = $(postLinks[i]).attr("href"); | |
jsonArray.push({"title" : title, "summary" : summary, "link" : link}); | |
} | |
res.send(200, jsonArray); | |
} | |
); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment