Last active
February 18, 2019 07:57
-
-
Save ryanburgess/99b4c31fd823106af5ad to your computer and use it in GitHub Desktop.
Example API using Express and Cheerio.
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
var express = require('express'); | |
var cheerio = require('cheerio'); | |
var request = require('request'); | |
var app = express(); | |
var wordOfDay = []; | |
app.get('/', function (req, res) { | |
// allow access from other domains | |
res.header('Access-Control-Allow-Origin', '*'); | |
res.header('Access-Control-Allow-Headers', 'X-Requested-With'); | |
// use Cheerio to make request | |
request({ | |
method: 'GET', | |
url: 'http://www.wordthink.com' | |
}, function(err, response, body, callback) { | |
if (err) return console.error(err); | |
// get the HTML body from WordThink.com | |
$ = cheerio.load(body); | |
if(wordOfDay.length > 0){ | |
wordOfDay = []; | |
} | |
var post = $('#content .singlemeta:first-child .post'); | |
var word = post.find('.title').eq(0).text().replace('\r\n\t\t\t\t\t', '').replace('\r\n\t\t\t\t', ''); | |
var definition = post.find('p').eq(0).text().replace('\n', ''); | |
// create an object | |
wordOfDay.push({word: word, definition: definition}) | |
}); | |
// return a JSON object as a response | |
res.send(JSON.stringify(wordOfDay, null, 4)); | |
}); | |
// start app on localhost port 3000 | |
var port = process.env.PORT || 3000; | |
app.listen(port, function () { | |
console.log('listening on port ' + port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment