Last active
February 26, 2017 18:24
-
-
Save maxrolon/3a5986ca8bab632b242b6d1c723c72c2 to your computer and use it in GitHub Desktop.
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 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