3

I am trying to experiment with AWS Lambda and I am using Serverless CLI for my deployment. I am using aws-nodejs template to generate my project folder.

This is the handler.js code:

'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. I am trying to tweak it to return HTML response. 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.

codingsplash
  • 4,785
  • 12
  • 51
  • 90

2 Answers2

4

You just need to add the content headers for 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.

Swaraj Giri
  • 4,007
  • 2
  • 27
  • 44
0

This works, try it. I have tested it with Lambda Function URL or with Lambda as Target for Application Load balancer

export const handler = async(event) => {
   
    // TODO implement
    const response = {
        statusCode: 200,
        body: '<h1>HTML from Lambda without API GW</h1>',
        headers: {
            'Content-Type': 'text/html',
        }
    };
    return response;
};
Mayur
  • 1
  • 1