rdlabo-team/serverless-nestjs

How do I access the event and context from a NestJs controller?

Closed this issue · 1 comments

The event and context are already accessible in index.ts but how to access them from an HTTP method in AppController?

This issue #4 event and context seem to be accessed using the usual nodejs express method.

I intended to access the event like is shown below but maybe through the Request itself if it had "req.apiGateway".

@Post("email")
async registerEmail(@Req() req: Request, @Body() email: RegisterEmailDTO, event: APIGatewayProxyEvent): Promise<string> {
	const ipaddress = event.requestContext.identity.sourceIp;
	console.log("ipaddress: " + ipaddress, email);

	const r = await this.registerService.registerEmail(email.email, ipaddress);
	return r;
}

They can be found in the headers:

	console.log(decodeURIComponent(req.headers["x-apigateway-event"].toString()));
	console.log(decodeURIComponent(req.headers["x-apigateway-context"].toString()));

Reference of how the proxy passes it through the header.