Everyone,
I'm trying to setup my first NestJS application. It is backed by Serverless on AWS.
I created a simple Controller that has a Service as a dependency. When I hit the endpoint with my HTTP Client, the object that should contain the Service instance is undefined. I'm not able to make it work. Could you help?
handler.ts
import { Context, Handler } from 'aws-lambda'; import { NestFactory } from '@nestjs/core'; import { AppModule } from './src/module'; import { Server } from 'http'; import { ExpressAdapter } from '@nestjs/platform-express'; import * as serverless from 'aws-serverless-express'; import * as express from 'express'; import {DB} from './src/libs/db'; let cachedServer: Server; function bootstrapServer(): Promise<Server> { const expressApp = express(); const adapter = new ExpressAdapter(expressApp); return NestFactory.create(AppModule, adapter) .then(app => app.enableCors()) .then(app => app.init()) .then(() => DB.connect()) .then(() => serverless.createServer(expressApp)); } export const handle: Handler = (event: any, context: Context) => { if (!cachedServer) { bootstrapServer().then(server => { cachedServer = server; return serverless.proxy(server, event, context); }); } else { return serverless.proxy(cachedServer, event, context); } }; module.ts
import { Module } from '@nestjs/common'; import { EventController } from './event.controller'; import { EventService } from './event.service'; @Module({ controllers: [EventController], providers: [EventService], }) export class AppModule {} event.service.ts
import { Injectable } from '@nestjs/common'; interface Event{} @Injectable() export class EventService { create(event: Event) { return { id: Date.now() } } } event.controller.ts
import { Controller, Post, Body } from '@nestjs/common'; import { EventService } from './event.service'; interface Event { } @Controller('event') export class EventController { constructor(private readonly eventService: EventService) { } @Post() async create(@Body() req) { this.eventService.create(req); } } So this.eventService is always undefined. What is wrong with this implementation?