Skip to content

Instantly share code, notes, and snippets.

@codingoutloud
Created September 14, 2017 03:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codingoutloud/33ceef887dfb186ebf9c683fa5a4cdb9 to your computer and use it in GitHub Desktop.
Save codingoutloud/33ceef887dfb186ebf9c683fa5a4cdb9 to your computer and use it in GitHub Desktop.
Simple "hello world"-level web page intended for use with Azure Functions - paste this text into a JavaScript HTTP trigger, upload project.json to kudu, and set up MOVIE_API_KEY as an environment variable.
var request = require('request');
module.exports = function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
var html = "<html><body><h1>didn't work</h1></body></html>"
var movieApiUrl = 'https://andruxnet-random-famous-quotes.p.mashape.com/?cat=movies&count=10';
// go get your own API key: https://market.mashape.com/andruxnet/random-famous-quotes#get-endpoint
var movieApiKey = process.env['MOVIE_API_KEY'];
// context.log('MOVIE API KEY = ' + movieApiKey);
var request = require('request');
var options = {
method: 'GET',
url: movieApiUrl,
headers: {
'X-Mashape-Key': movieApiKey,
'Accept': 'application/json'
}
};
function callback(error, response, body) {
context.log(' statuc code => ' + response.statusCode);
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
html = '<html><body><h1>' + info[0].quote + '</h1> <h2 style="color:white;"><i>' + info[0].author + '</i></h2></body></html>';
//if (req.query.name || (req.body && req.body.name)) {
context.res = {
// status: 200, /* Defaults to 200 */
headers: {
'Content-Type': 'text/html; charset=utf-8'
},
body: html
};
//}
context.done();
}
else {
context.log('* * * * error ' + error);
context.res = {
status: 400,
body: "Please pass a name on the query string or in the request body"
};
context.done();
}
}
request(options, callback);
};
{
"name": "randommoviequotes",
"version": "0.0.0",
"private": true,
"dependencies": {
"request": "~2.81.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment