Created
March 2, 2018 16:47
-
-
Save jonasgroth/3d1dab630140036b2273e8e08725c03c 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
console.log('Loading event'); | |
var doc = require('dynamodb-doc'); | |
var dynamodb = new doc.DynamoDB(); | |
exports.handler = function(event, context, callback) { | |
console.log("Request received:\n", JSON.stringify(event)); | |
var tableName = "jonasgroth.se_comments"; | |
var post = event.queryStringParameters.post; | |
dynamodb.query({ | |
TableName : tableName, | |
KeyConditionExpression: "#post = :post", | |
ExpressionAttributeNames:{ | |
"#post": "post" | |
}, | |
ExpressionAttributeValues: { | |
":post":post | |
}, | |
Limit : 30 | |
}, function(err, data) { | |
if (err) { | |
context.done('error','reading dynamodb failed: '+err); | |
var body = []; | |
} else { | |
var body = []; | |
for(var item in data.Items){ | |
var comment = data.Items[item]; | |
body.push({authorName: comment.authorName, timedate: comment.timedate, commentText: comment.commentText}); | |
} | |
} | |
var httpResponse = { | |
"statusCode":200, | |
"headers": { | |
"date": new Date().toUTCString(), | |
"X-Requested-With":"*", | |
"Access-Control-Allow-Headers":"Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token", | |
"access-control-allow-origin":"https://jonasgroth.se", | |
"access-control-allow-methods": "DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT", | |
"content-type": "application/json", | |
"status": 200, | |
"content-length": body.length | |
}, | |
"body": JSON.stringify(body) | |
}; | |
console.log("Response: " + JSON.stringify(httpResponse)); | |
callback(null, httpResponse); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment