简体   繁体   中英

How to return HTML code from Lambda function in NodeJS?

I have the following Lambda function.

I need to return some custom html when the function is called.

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. We can't connect to the server for this app or website at this time.

I took the code from AWS blueprint:

在此处输入图像描述

Original code from AWS:

在此处输入图像描述

Does anyone know what I'm doing wrong please?

Thanks. Cheers,

You appear to have used the regular AWS Lambda blueprint. Edge Lambda functions are different eg the status code is returned in status , not in 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);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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