0

I am studying serverless nestjs.

I am trying to run serverless offline using the serverless-offline plugin.

I've succeeded in running serverless offline, but when I send a server request it doesn't respond.

The serverless message looks like this

C:\study\nestjs-practice\serverless-nestjs> serverless offline start
Serverless: Deprecation warning: Starting from next major object notation for "service" property will no longer be recognized. Set "service" property directly with service name.
            More Info: https://www.serverless.com/framework/docs/deprecations/#SERVICE_OBJECT_NOTATION
Serverless: Deprecation warning: Resolution of lambda version hashes was improved with better algorithm, which will be used in next major release.
            Switch to it now by setting "provider.lambdaHashingVersion" to "20201221"
            More Info: https://www.serverless.com/framework/docs/deprecations/#LAMBDA_HASHING_VERSION_V2
Serverless: Compiling with Typescript...
Serverless: Using local tsconfig.json
Serverless: Typescript compiled.
Serverless: Watching typescript files...
offline: Starting Offline: dev/us-east-1.
offline: Offline [http for lambda] listening on http://localhost:3002
offline: Function names exposed for local invocation by aws-sdk:
           * main: serverless-nestjs-dev-main

   ┌────────────────────────────────────────────────────────────────────────┐
   │                                                                        │
   │   ANY | http://localhost:3000/dev/{any*}                               │
   │   POST | http://localhost:3000/2015-03-31/functions/main/invocations   │
   │                                                                        │
   └────────────────────────────────────────────────────────────────────────┘

offline: [HTTP] server ready: http://localhost:3000 
offline: 
offline: Enter "rp" to replay the last request

offline: ANY /dev (λ: main)
[Nest] 9408   - 2021. 08. 18. 오전 8:55:44   [NestFactory] Starting Nest application...
[Nest] 9408   - 2021. 08. 18. 오전 8:55:44   [InstanceLoader] AppModule dependencies initialized +39ms
[Nest] 9408   - 2021. 08. 18. 오전 8:55:44   [RoutesResolver] AppController {}: +8ms
[Nest] 9408   - 2021. 08. 18. 오전 8:55:44   [RouterExplorer] Mapped {, GET} route +5ms
[Nest] 9408   - 2021. 08. 18. 오전 8:55:44   [NestApplication] Nest application successfully started +6ms

Executed successfully, but no response to the request.

I sent a request to http://localhost:3000/dev uri to hello api of app.controller in nestjs which is created by default. The http method used GET.

I'll attach the code, let me know if you have any problems.

lambda.ts

import { Handler, Context } from 'aws-lambda';
import { Server } from 'http';
import { createServer, proxy } from 'aws-serverless-express';
import { eventContext } from 'aws-serverless-express/middleware';
import { NestFactory } from '@nestjs/core';
import { ExpressAdapter } from '@nestjs/platform-express';
import { AppModule } from './app.module';
import * as express from 'express';

const binaryMimeTypes: string[] = [];

let cachedServer: Server;

async function bootstrapServer(): Promise<Server> {
  if (!cachedServer) {
    const expressApp = express();
    const nestApp = await NestFactory.create(
      AppModule,
      new ExpressAdapter(expressApp),
    );
    nestApp.use(eventContext());
    await nestApp.init();
    cachedServer = createServer(expressApp, undefined, binaryMimeTypes);
  }
  return cachedServer;
}

export const handler: Handler = async (event: any, context: Context) => {
  cachedServer = await bootstrapServer();
  return proxy(cachedServer, event, context, 'PROMISE').promise;
};

serverless.yml

service:
  name: serverless-nestjs

plugins:
  - serverless-plugin-typescript
  - serverless-plugin-optimize
  - serverless-offline

provider:
  name: aws
  runtime: nodejs12.x

functions:
  main:
    handler: src/lambda.handler
    events:
      - http:
          method: any
          path: /{any+}

yangseungbin
  • 101
  • 6
  • Just to add to the question, when I send a request using postman, the Sending Request pops up forever. When I make a request from the web, I get a good response. – yangseungbin Aug 18 '21 at 00:15

1 Answers1

0

Would be helpful to see app.controller.ts. I think that you have a method inside your

app.controller.ts

, that should have a decorator above it, like this:

@Get('hello')
getHello() {
return this.appService.getHello();
}

, following this code sample you would have to request http://localhost:3000/dev/hello.

Stefan Drl
  • 53
  • 7