Skip to content

Instantly share code, notes, and snippets.

@maxrolon
Last active February 26, 2017 18:24
Show Gist options
  • Save maxrolon/3a5986ca8bab632b242b6d1c723c72c2 to your computer and use it in GitHub Desktop.
Save maxrolon/3a5986ca8bab632b242b6d1c723c72c2 to your computer and use it in GitHub Desktop.
var params = {
"method": "GET",
"hostname": "www.<URL>.com",
"port": null,
"headers": {
"cache-control": "no-cache"
}
};
var secret = process.env.SECRETENV;
exports.handler = (event, context, callback) => {
var qs = event['queryStringParameters'] //Gets parameters sent through the API Endpoint
qs = Object.keys( qs ).map( key => `${key}=${qs[key]}` ).join('&') //Adds parameters as a query string to URL
params['headers']['secret'] = secret //Adds secret as header parameter
params['path'] = '/url-path?' + qs //Adds query string to URL
var req = http.request(params, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
parseString(body.toString(), function (err, result) {
//This is an AWS Lambda convention to end a function
context.succeed( {
"statusCode": 200,
"headers": {
'Access-Control-Allow-Origin': '*'
},
"body": JSON.stringify(result)
} ); //Ends Lambda function by returning API response
});
});
});
req.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment