Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** This lambda function will proxy the request to another endpoint, easily allowing local execution of the real implemenation. */
- const https = require('https');
- exports.handler = function(event, context, callback) {
- console.log(`Connecting to ${process.env.HOSTNAME}${process.env.PATH}.`);
- const postData = JSON.stringify({event, context});
- const options = {
- hostname: process.env.HOSTNAME,
- port: 443,
- path: process.env.PATH,
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'Content-Length': postData.length
- }
- };
- const req = https.request(options, resp => {
- console.log('Waiting for response.');
- let data = '';
- resp.on('data', block => {
- console.log(`New data: ${block}.`);
- data+=block;
- });
- resp.on('end', () => {
- console.log(`Obtained data: ${data}.`);
- const implResponse = JSON.parse(data);
- callback(implResponse.err, implResponse.response);
- });
- }).on("error", err => {
- console.log(`Error: %s.`, JSON.stringify(err));
- callback(err);
- });
- req.write(postData);
- req.end();
- };
- /** Here it is the other side of the coin: the proxied actual client. */
- const isAWSLambda = process.env.LAMBDA_TASK_ROOT && process.env.AWS_EXECUTION_ENV;
- if (!isAWSLambda) {
- console.log('Local execution detected. Launching server.');
- var express = require('express');
- var bodyParser = require('body-parser');
- var app = express();
- app.use(bodyParser.json());
- app.post('/', function(req, res) {
- console.log(`New request %s.`, JSON.stringify(req.body));
- exports.handler(req.body.event, req.body.context, (err, response) => {
- const skillResponse = {err, response};
- console.log(`Skill response: %s.`, JSON.stringify(skillResponse));
- res.setHeader('Content-Type', 'application/json');
- res.send(JSON.stringify(skillResponse));
- });
- });
- app.listen(80, function () {
- console.log('Server listening on port 80.');
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment