Skip to content

Instantly share code, notes, and snippets.

@danilop
Last active May 7, 2023 15:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danilop/939df8b4de06eb3a0bc6ae0ca1d9387d to your computer and use it in GitHub Desktop.
Save danilop/939df8b4de06eb3a0bc6ae0ca1d9387d to your computer and use it in GitHub Desktop.
Sample AWS Lambda function exposing the same business logic using the Amazon API Gateway of the Node.js Express framework.
"use strict";
console.log('Loading function');
// Your business logic
function greetingsFor(name) {
console.log('name: ', name);
if ((name == undefined) || (name == '')) {
name = 'World';
}
const greetings = 'Hello ' + name + '!';
console.log('greetings: ', greetings);
return greetings;
}
// Event wrapper for Amazon API Gateway
exports.handler = async (event, context) => {
function buildResponse(message) {
const responseCode = 200;
const responseBody = {
message: message
};
const response = {
statusCode: responseCode,
headers: {
'x-custom-header' : 'my custom header value'
},
body: JSON.stringify(responseBody)
};
console.log('response: ' + JSON.stringify(response))
return response;
}
console.log('request: ' + JSON.stringify(event));
let name; // default value is undefined
if (event.queryStringParameters != null) {
name = event.queryStringParameters.name;
}
return buildResponse(greetingsFor(name));
};
// If not running on AWS Lambda, use Express
if (!(process.env.LAMBDA_TASK_ROOT && process.env.AWS_EXECUTION_ENV)) {
const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send(greetingsFor(req.query.name));
})
app.listen(process.env.PORT || 3000)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment