Last active
August 2, 2018 03:50
-
-
Save patbeagan1/36c15bd9967e5b68d4ed9b67c5710d5b to your computer and use it in GitHub Desktop.
Grabbing a drink and image from thecocktaildb
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
/** | |
* https://www.thecocktaildb.com/api.php | |
* Note, this is for educational purposes only, request an api key for other uses. | |
*/ | |
var https = require('https'); | |
var express = require('express') | |
function slurpJson(optionInfo) { | |
return new Promise(function(resolve, reject) { | |
var reqGet = https.request(optionInfo, | |
function(res) { | |
console.log("statusCode: ", res.statusCode); | |
var inProgressData = ""; | |
res.on("data", function(data) { | |
inProgressData += data; | |
}); | |
res.on("end", function() { | |
var result = JSON.parse(inProgressData); | |
resolve(result) | |
}); | |
}); | |
reqGet.end(); | |
reqGet.on('error', function(e) { | |
console.error(e); | |
reject(Error(e)) | |
}); | |
}) | |
} | |
function getDrinkInfo(id) { | |
return slurpJson({ | |
host: 'www.thecocktaildb.com', | |
port: 443, | |
path: '/api/json/v1/1/lookup.php?i=' + id, | |
method: 'GET' | |
}) | |
} | |
function getRandomDrink() { | |
return new Promise(function(resolve, reject) { | |
slurpJson({ | |
host: 'www.thecocktaildb.com', | |
port: 443, | |
path: '/api/json/v1/1/random.php', | |
method: 'GET' | |
}).then(it => { | |
getDrinkInfo(it.drinks[0].idDrink).then(it => { | |
resolve(it) | |
}, err => { | |
reject(err) | |
}) | |
}, err => { | |
reject(err) | |
}) | |
}) | |
} | |
var app = express() | |
app.get('/', (req, res) => { | |
getRandomDrink().then(it => { | |
let drink = it.drinks[0] | |
res.send('<p>' + drink.strDrink + '</p>' + | |
'<img src="' + drink.strDrinkThumb + '"/>') | |
}, err => { | |
console.log(err) | |
}) | |
}) | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment