简体   繁体   English

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

[英]Returning HTML response without API gateway in AWS Lambda for Node template

I am trying to experiment with AWS Lambda and I am using Serverless CLI for my deployment.我正在尝试使用 AWS Lambda,并且我正在使用无服务器 CLI 进行部署。 I am using aws-nodejs template to generate my project folder.我正在使用 aws-nodejs 模板来生成我的项目文件夹。

This is the handler.js code:这是 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 };
};

I am getting a successful response in JSON format.我得到了 JSON 格式的成功响应。 I am trying to tweak it to return HTML response.我正在尝试调整它以返回 HTML 响应。 Should I change the content-type for that?我应该为此更改内容类型吗? If so how?如果是这样怎么办?

I have gone through the below questions:我已经完成了以下问题:

and a few others as well.还有其他一些人。 But all of them are using the web console and the API gateway which I am not using.但是他们都在使用我没有使用的 Web 控制台和 API 网关。

You just need to add the content headers for html您只需要为 html 添加内容标题

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

Also, this is in one of the serverless examples in their github repo .此外,这是在他们的 github repo中的无服务器示例之一

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

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