最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - HTML not rendering when returned by AWS Lambda function - Stack Overflow

programmeradmin3浏览0评论

I'm currently attempting to serve a very basic static webpage when a lambda function is invoked through it's function URL (not through API Gateway). However, when I return the response for the request, it is still being rendered as text by the browser.

I am using SST for orchestration, with the lambda handler written in TypeScript (Node.js). Lambda handler code:

export const handler = async (event: any, context: any) => {
  console.log("Handler invoked");

  let content = `
<\!DOCTYPE html>
<html lang="en">
  <body>
    <h1>Hello world</h1>
    <p>Hello from my example</p>
  </body>
</html>
`;

  let response = {
    status: 200,
    headers: {
      "Content-Type": "text/html",
    },
    body: content,
  };

  return response;
};

I'm currently attempting to serve a very basic static webpage when a lambda function is invoked through it's function URL (not through API Gateway). However, when I return the response for the request, it is still being rendered as text by the browser.

I am using SST for orchestration, with the lambda handler written in TypeScript (Node.js). Lambda handler code:

export const handler = async (event: any, context: any) => {
  console.log("Handler invoked");

  let content = `
<\!DOCTYPE html>
<html lang="en">
  <body>
    <h1>Hello world</h1>
    <p>Hello from my example</p>
  </body>
</html>
`;

  let response = {
    status: 200,
    headers: {
      "Content-Type": "text/html",
    },
    body: content,
  };

  return response;
};

And the sst.config.ts:

new sst.aws.Function("UploadPage", {
  handler: "src/lambda/lambda.handler",
  url: true,
});

When invoked, the browser response looks like this in Chrome:

And like this when invoked in firefox:

I have noted that chrome appears to be showing a lot of escaped characters (particularly of note under the Content-Type header) which led me to believe this was the issue, however firefox doesn't appear to show this and also doesn't render the HTML as expected.

What do i need to change within my code to make the browsers understood that they need to render the code?

Share Improve this question asked Feb 7 at 13:34 carbarettacarbaretta 3241 silver badge6 bronze badges 2
  • You'll probably have to return actual HTML, instead of a JSON data structure. Or you will have to find a way to access the content of the body property in your JSON, and make the browser render only that. – C3roe Commented Feb 7 at 13:45
  • @C3roe I believe this would work if I were utilising API gateway, but not for URL invocation. API gateway transforms and wraps responses which raw URL invocations don't. I'm avoiding API gateway to avoid overhead & added complexity. – carbaretta Commented Feb 7 at 13:53
Add a comment  | 

1 Answer 1

Reset to default 0

Very simple fix for this, which isn't mentioned anywhere in documentation. When returning the response, you need statusCode, not status.

Using the following response instead resolved the issue:

  let response = {
    statusCode: 200,
    headers: {
      "Content-Type": "text/html",
    },
    body: content,
  };

发布评论

评论列表(0)

  1. 暂无评论