CodeGenieApp / CodeGenieApp/serverless-express

Dependencies not being injected on NestJS with Serverless stack and AWS

Open
#390 9 comments 5 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
5.3k
Forks
676
PR merge metrics
No merged PRs in 30d

Description

We are trying to integrate the NestJs with serverless stack and AWS . We have simple Controller that has a Service as a dependency.
when we make request to the controller the serviceObject that should contain the Service instance instead gives us undefined .

Below is our app.module.ts file contain

`

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
controllers: [AppController],
providers: [AppService,
],
})
export class AppModule {}
`

Lambda.ts contain
`

import { NestFactory } from '@nestjs/core';
import { ExpressAdapter } from '@nestjs/platform-express';
import serverlessExpress from '@vendia/serverless-express';
import { Context, Handler } from 'aws-lambda';
import express from 'express';

import { AppModule } from './app.module';

let cachedServer: Handler;

async function bootstrap() {
if (!cachedServer) {
const expressApp = express();
const nestApp = await NestFactory.create(
AppModule,
new ExpressAdapter(expressApp),
);

nestApp.enableCors();

await nestApp.init();

cachedServer = serverlessExpress({ app: expressApp });
}

return cachedServer;
}

export const handler = async (event: any, context: Context, callback: any) => {
const server = await bootstrap();
return server(event, context, callback);
};

`

Main.ts

`

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);

await app.listen(3000);
}

bootstrap();

`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.