【问题标题】:I sent a server request to serverless nestjs, but no response我向无服务器的nestjs发送了一个服务器请求,但没有响应
【发布时间】:2023-03-07 01:08:02
【问题描述】:

我正在研究无服务器的nestjs。

我正在尝试使用 serverless-offline 插件离线运行无服务器。

我已经成功离线运行无服务器,但是当我发送服务器请求时它没有响应。

无服务器消息如下所示

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

执行成功,但没有响应请求。

我向 http://localhost:3000/dev uri 发送了一个请求到默认创建的 Nestjs 中 app.controller 的 hello api。 http 方法使用 GET。

我会附上代码,如果您有任何问题,请告诉我。

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+}

【问题讨论】:

  • 只是为了补充问题,当我使用邮递员发送请求时,发送请求会永远弹出。当我从网络发出请求时,我得到了很好的响应。

标签: typescript lambda nestjs serverless serverless-offline


【解决方案1】:

查看 app.controller.ts 会有所帮助。 我认为您的内部有一个方法

app.controller.ts

,上面应该有一个装饰器,像这样:

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

,按照此代码示例,您必须请求 http://localhost:3000/dev/hello。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-20
    • 2019-07-19
    • 1970-01-01
    • 2019-03-30
    • 1970-01-01
    • 2017-01-28
    • 2019-12-30
    • 2013-11-01
    相关资源
    最近更新 更多