Modifying handler to return response body when certain endpoint is requested when using serverless framework

Viewed 124

I am using serverless framework to deploy entire nodejs type-script app on AWS Lambda.

When an endpoint is requested it invokes the lambda function, after which function corresponding to that endpoint is called and gets executed. I am certain that the lambda is able to get to that function as I can see the console log present in that function in CloudWatch logs.

From my backend code, lambda receives the statuscode 200 but does not returns the response body.

My serverless file,

functions:
  index:
    handler: src/index.handler
    events:
      - http:
          path: /
          method: ANY
          cors: true
      - http:
          path: /{proxy+}
          method: ANY
          cors: true

This is my handler which is in index.ts

let databaseconnect = async() =>{

  if(!DatabaseService.isConnected)
  await DatabaseService.getConnection()
...
...
...
  const server = awsServerlessExpress.createServer(app)
  return server
  
}

exports.handler = async(event: any, context: any) => {

  let server = await databaseconnect()
  awsServerlessExpress.proxy(server, event, context)

} 

My router,

router.get('/check',(req:Request,res: Response)=>{
    return res.status(200).json({message: "Its working one."});
});


// //router.get("/abc",abc)
router.post("/abc",Controller.abc)

My controller,

...
...
...
public static abc = async (req:Request,res:Response,next:any) => {
  console.log("ABC controller")
  return res.status(200).json({message:JSON.stringify("get request")})
}


}
export default Controller;

The mentioned console log is visible on CloudWatch logs

logs,

[32minfo[39m: POST /api/v1/user/abc 200 0ms 
{
    "meta": {
        "req": {
            "headers": {
                "connection": "close",
                "content-length": "2",
                "host": "localhost",
                "x-apigateway-context": "%7B%22callbackWaitsForEmptyEventLoop%22%3Atrue%2C%22functionVersion%22%3A%22%24LATEST%22%2C%22functionName%22%3A%22lambda-lam-test-index%22%2C%22memoryLimitInMB%22%3A%224096%22%2C%22logGroupName%22%3A%22%2Faws%2Flambda%2Flambda-lam-test-index%22%2C%22logStreamName%22%3A%222022%2F06%2F21%2F%5B%24LATEST%5Deb2438ba18024c8a8f7457ae221b0cee%22%2C%22invokedFunctionArn%22%3A%22arn%3Aaws%3Alambda%3Aap-south-1%3A945159732863%3Afunction%3Alambda-lam-test-index%22%2C%22awsRequestId%22%3A%22865999b5-d830-45cb-bd3e-4dc56702c5eb%22%7D",
                "x-apigateway-event": "%7B%22resource%22%3A%22%2F%7Bproxy%2B%7D%22api%2Fv1%2Fuser%2Fabc%22%2C%22httpMethod%ltiValueHeaders%22%3Anull%2C%22queryStringParameters%22%3Anull%2C%22multiValueQueryStringParameters%22%3Anull%2C%22pathParameters%22%3A%7B%22proxy%22%3A%23A%2221%2FJun%2F2022%3A14%3A28%3A50%20%2B0000%22%2C%22path%22%3A%22%2F%7Bproxy%2B%7D%22%2C%22accountId%22%3A%22945159732863%22%2C%22protocol%22%3A%22HTTP%2F1.1%22%2C%22stage%22%3A%22test-invoke-stage%22%2C%22domainPrefix%22%3A%22testPrefix%22%2C%22requestTimeEpoch%22%3A1655821730840%2C%22requestId%22%3A%22433bc178-8a53-47b7-9ca5-2a6cf8b31c02%22%2C%22identity%22%3A%7B%22cognitoIdentityPoolId%22%3Anull%2C%22cognitoIdentityId%22%3Anull%2C%22apiKey%22%3A%22test-invoke-api-key%22%2C%22principalOrgId%22%3Anull%2C%22cognitoAuthenticationType%22%3Anull%2C%22userArn%22%3A%22arn%3Aaws%3Aiam%3A%3A945159732863%3Auser%2Fharsh%22%2C%22apiKeyId%22%3A%22test-invoke-api-key-id%22%2C%22userAgent%22%3A%22aws-internal%2F3%20aws-sdk-java%2F1.12.201%20Linux%2F5.4.186-113.361.amznlomainName%22%3A%22testPrefix.testDomainName%22%2C%22apiId%22%3A%22vzfqx2hdx8%22%7D%2C%22isBase64Encoded%22%3Afalse%7D"
            },
            "httpVersion": "1.1",
            "method": "POST",
            "originalUrl": "/api/v1/user/abc",
            "query": {},
            "url": "/api/v1/user/abc"
        },
        "res": {
            "statusCode": 200
        },
        "responseTime": 0
    }
}

.........8D5999b5-d8F0-45cF-FWA3e-4dc5WFA2c5eb  INFO    ABC controller

What modification should be made to return the response body along with the statuscode?

0 Answers
Related