简体   繁体   English

如何在 NodeJS 中从 Lambda function 返回 HTML 代码?

[英]How to return HTML code from Lambda function in NodeJS?

I have the following Lambda function.我有以下 Lambda function。

I need to return some custom html when the function is called.当调用 function 时,我需要返回一些自定义 html。

I tried:我试过了:

    exports.handler = async (event, context, callback) => {   
        const response = {
            statusCode: 200,
            headers: {
                'Content-Type': 'text/html',
            },
            body: String("Hi there !"),
        };
        return response;
    }

But when invoking the function, I'm getting the following error: The Lambda function returned an invalid entry in the headers object: Each header entry in the headers object must be an array.但是在调用 function 时,出现以下错误: Lambda function 在标头 object 中返回了一个无效条目:标头 object 中的每个 header 条目都必须是一个数组。 We can't connect to the server for this app or website at this time.我们目前无法连接到此应用程序或网站的服务器。

I took the code from AWS blueprint:我从 AWS 蓝图中获取了代码:

在此处输入图像描述

Original code from AWS:来自 AWS 的原始代码:

在此处输入图像描述

Does anyone know what I'm doing wrong please?请问有人知道我做错了什么吗?

Thanks.谢谢。 Cheers,干杯,

You appear to have used the regular AWS Lambda blueprint.您似乎使用了常规的 AWS Lambda 蓝图。 Edge Lambda functions are different eg the status code is returned in status , not in statusCode . Edge Lambda 功能不同,例如状态代码在status中返回,而不是在statusCode中返回。

Based on the documented example :基于文档示例

exports.handler = (event, context, callback) => {
    const response = {
        status: '200',
        statusDescription: 'OK',
        headers: {
            'cache-control': [{
                key: 'Cache-Control',
                value: 'max-age=100'
            }],
            'content-type': [{
                key: 'Content-Type',
                value: 'text/html'
            }]
        },
        body: "some HTML content here",
    };
    callback(null, response);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM