在AWS Lambda for Node模板中返回没有API网关的HTML响应

时间:2019-03-14 06:19:06

标签: node.js aws-lambda

我正在尝试使用AWS Lambda,并且正在使用无服务器CLI进行部署。我正在使用aws-nodejs模板生成我的项目文件夹。

这是handler.js代码:

'use strict';

module.exports.hello = async (event, context) => {
  return {
    statusCode: 200,
    body: {
      "message":
      'Hello World! Today is '+new Date().toDateString()
    }
  };

  // Use this code if you don't use the http event with the LAMBDA-PROXY integration
  // return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};

我收到JSON格式的成功响应。我试图对其进行调整以返回HTML响应。我应该为此更改内容类型吗?如果可以,怎么办?

我经历了以下问题:

以及其他一些。但是他们所有人都在使用Web控制台和我未使用的API网关。

1 个答案:

答案 0 :(得分:1)

您只需要添加html的内容标头

return {
  statusCode: 200,
  headers: {
    'Content-Type': 'text/html',
  },
  body: '<p>Look ma, its sending html now</p>',
}

此外,它在serverless examples in their github repo之一中。

相关问题