Skip to content

Instantly share code, notes, and snippets.

@krstoff
Last active May 28, 2017 23:08
Show Gist options
  • Save krstoff/b45d39168593fca12de636bb26cf49ef to your computer and use it in GitHub Desktop.
Save krstoff/b45d39168593fca12de636bb26cf49ef to your computer and use it in GitHub Desktop.
'use strict';
var request = require('request-promise');
var P = require('bluebird');
// Home page
module.exports.home = (event, context, callback) => {
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'Go Serverless v1.0! Your function executed successfully!',
input: event,
}),
};
callback(null, response);
};
/// Handler for /travel/{url}
module.exports.travel = (event, context, callback) => {
// Get the url from the resource path.
// Get the html.
// Turn it into a successful request.
// Attach the Node.js callback onto the end.
P.try( () => {
var url = getURLFromPath(event.path);
var htmlPromise = request('http://' + url);
var response = htmlPromise.then(make200Reponse);
return response;
}).catch(make500Response).asCallback(callback);
}
// Assumes /travel/{path}
function getURLFromPath(path) {
return path.substring(8);
}
// Takes an html body and wraps it in a 200 response
function make200Reponse(body) {
return {
statusCode: 200,
headers: {
'Content-Type': 'text/html',
'Access-Controll-Allow-Origin': "*",
},
body: html,
}
}
function make500Response(err) {
return {
statusCode: 500,
headers: {
'Content-Type': 'text/html',
'Access-Controll-Allow-Origin': "*",
},
body: JSON.stringify(err),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment