- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个 Lambda@Edge 函数来为我的 Angular SPA 应用程序返回 Open Graph HTML。我已将它安装到 CloudFrond“查看器请求”生命周期中。这个 lambda 检查用户代理,如果它是 Facebook 或 Twitter 爬虫,它返回 HTML(目前硬编码在 lambda 中用于测试)。如果请求来自任何其他用户代理,则该请求将传递到源。传递逻辑工作正常,但如果我尝试拦截并返回爬虫的 Open Graph HTML,则会出现错误。
在CloudWatch中,CloudFront报错是:
ERROR Validation error: The Lambda function returned an invalid body,body should be of object type.
在 Postman 中(通过伪造用户代理),我得到一个 502:
The Lambda function result failed validation: The body is not astring, is not an object, or exceeds the maximum size.
我用这个把我的头发拔掉了。有任何想法吗?这是我的 lambda。
'use strict';
function buildReleaseResponse( request ) {
const content = `<\!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta property="og:type" content="website" />
<meta property="og:title" content="Hello, World" />
<meta property="og:description" content="This is an Open Graph test" />
<meta property="og:image" content="https://cdn.website.com/photos/SGmzAlNmwOGpnMeijMoW9.jpg" />
<meta property="og:url" content="https://www.website.com/hello-world" />
<title>Hello, World</title>
</head>
<body>
<h1>Open Graph Test</h1>
</body>
</html>`;
return {
statusCode: 200,
statusDescription: 'OK',
headers: {
"content-type": [
{
"key": "Content-Type",
"value": "text/html; charset=utf-8"
}
]
},
body: content.toString()
};
}
exports.handler = ( event, context, callback ) => {
const { request, response } = event.Records[0].cf;
let userAgentStr = "";
if (request.headers['user-agent']) {
if (request.headers['user-agent'].length > 0) {
userAgentStr = request.headers['user-agent'][0].value;
}
}
let newResponse = null;
if ( userAgentStr.match(/facebookexternalhit|twitterbot/i) ) {
if ( request.uri.startsWith("/radio/release/") ) {
newResponse = buildReleaseResponse(request);
}
}
if ( newResponse === null ) {
console.log("Passthrough.");
callback(null, request);
}
else {
console.log("Overriding response with: " + JSON.stringify(newResponse));
callback(null, newResponse);
}
};
这是 cloudwatch (conole.log) 中显示的响应
{
"statusCode": 200,
"statusDescription": "OK",
"headers": {
"content-type": [{
"key": "Content-Type",
"value": "text/html; charset=utf-8"
}],
"cache-control": [{
"key": "Cache-Control",
"value": "max-age=100"
}]
},
"body": "<!DOCTYPE html>\n <html lang=\"en\">\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n \n <meta property=\"og:type\" content=\"website\" />\n <meta property=\"og:title\" content=\"Yo Dog\" />\n <meta property=\"og:description\" content=\"Song by Raf Fiol and Mike Patterson\" />\n <meta property=\"og:image\" content=\"https://cdn.kompoz.com/photos/SGmzAlNmwOGpnMeijMoW9.jpeg\" />\n <meta property=\"og:url\" content=\"https://3-www.kompoz.com/radio/release/yo_dog\" />\n \n <title>Kompoz.com</title>\n </head>\n <body>\n <h1>Yo Dog</h1>\n </body>\n </html>"
}
这是 CloudFront Lambda 配置
最佳答案
已解决!我很尴尬地报告这个问题是由我的错字引起的。在我的响应对象中,我有:
"statusCode": 200,
但它应该是:
"status": 200,
很高兴地报告它现在正在运行。话虽如此,我希望 AWS 错误消息更好。消息“正文不是字符串,不是对象,或者超过最大大小”真的让我失望了。
关于angular - AWS Lambda@Edge Viewer 请求失败,返回 'The body is not a string, is not an object, or exceeds the maximum size',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69843453/
我是一名优秀的程序员,十分优秀!