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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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;
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.