英文:
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的蓝图中取得了这段代码:
AWS原始代码:
我做错了什么?
英文:
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 :
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);
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。




评论