如何从NodeJS中的Lambda函数返回HTML代码?

huangapple go评论63阅读模式
英文:

How to return HTML code from Lambda function in NodeJS?

问题

我有以下的Lambda函数。当调用该函数时,我需要返回一些自定义的HTML。

我尝试了:

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

但是在调用该函数时,我收到以下错误信息:Lambda函数在headers对象中返回了无效的条目:headers对象中的每个头条目必须是一个数组。我们无法连接到此应用程序或网站的服务器。

我从AWS的蓝图中取得了这段代码:

如何从NodeJS中的Lambda函数返回HTML代码?

AWS原始代码:

如何从NodeJS中的Lambda函数返回HTML代码?

我做错了什么?

英文:

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 :

如何从NodeJS中的Lambda函数返回HTML代码?

Original code from AWS :

如何从NodeJS中的Lambda函数返回HTML代码?

What am I doing wrong?

答案1

得分: 3

您似乎使用了常规的AWS Lambda蓝图。Edge Lambda函数不同,例如状态码在 status 中返回,而不是 statusCode

根据文档中的示例

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: "这里是一些HTML内容",
    };
    callback(null, response);
};
英文:

You appear to have used the regular AWS Lambda blueprint. Edge Lambda functions are different e.g. 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);
};

huangapple
  • 本文由 发表于 2023年1月9日 00:05:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049284.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定