Skip to content

Instantly share code, notes, and snippets.

@neilsmind
Last active August 25, 2017 21:50
Show Gist options
  • Save neilsmind/4001a3f2dda4517c802491dbea226aa8 to your computer and use it in GitHub Desktop.
Save neilsmind/4001a3f2dda4517c802491dbea226aa8 to your computer and use it in GitHub Desktop.
Code issue with AWS SAM Local example in docs
'use strict';
// http://docs.aws.amazon.com/lambda/latest/dg/test-sam-local.html#sam-cli-simple-app
// This is the corrected code for the url above. Specifically, the example for
// product.js does not return`body`, `header`, nor `statusCode` for GET, PUT and
// DELETE methods. Original code is included at the bottom.
exports.handler = (event, context, callback) => {
let id = event.pathParameters.product || false;
switch(event.httpMethod){
case "GET":
if(id) {
callback(null, { body: "This is a READ operation on product ID " + id )};
return;
}
callback(null, { body: "This is a LIST operation, return all products" });
break;
case "POST":
callback(null, { body: "This is a CREATE operation" });
break;
case "PUT":
callback(null, { body: "This is a UPDATE operation on product ID " + id });
break;
case "DELETE":
callback(null, { body: "This is a DELETE operation on product ID " + id });
break;
default:
// Send HTTP 501: Not Implemented
console.log("Error: unsupported HTTP method (" + event.httpMethod + ")");
callback(null, { statusCode: 501 })
}
}
/*
'use strict';
exports.handler = (event, context, callback) => {
let id = event.pathParameters.product || false;
switch(event.httpMethod){
case "GET":
if(id) {
callback(null, "This is a READ operation on product ID " + id);
return;
}
callback(null, "This is a LIST operation, return all products");
break;
case "POST":
callback(null, {body: "This is a CREATE operation"});
break;
case "PUT":
callback(null, "This is a UPDATE operation on product ID " + id);
break;
case "DELETE":
callback(null, "This is a DELETE operation on product ID " + id);
break;
default:
// Send HTTP 501: Not Implemented
console.log("Error: unsupported HTTP method (" + event.httpMethod + ")");
callback(null, { statusCode: 501 })
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment