网站搜索

node.js - nest js this.instance.use 不是一个函数


我正在学习使用 AWS Lambda 的无服务器 Nest.js

当我运行 serverless offline 并发送请求时,发生了错误。

[Nest] 11048   - 2021. 07. 14. 오후 7:02:11   [NestFactory] Starting Nest application...
[Nest] 11048   - 2021. 07. 14. 오후 7:02:11   [InstanceLoader] AppModule dependencies initialized +16ms
[Nest] 11048   - 2021. 07. 14. 오후 7:02:11   [ExceptionHandler] this.instance.use is not a function +3ms
TypeError: this.instance.use is not a function
    at ExpressAdapter.use (C:\study\nestjs-practice\serverless-nestjs\node_modules\@nestjs\core\adapters\http-adapter.js:20:30)
    at NestApplication.use (C:\study\nestjs-practice\serverless-nestjs\node_modules\@nestjs\core\nest-application.js:140:26)
    at C:\study\nestjs-practice\serverless-nestjs\node_modules\@nestjs\core\nest-factory.js:127:40
    at Function.run (C:\study\nestjs-practice\serverless-nestjs\node_modules\@nestjs\core\errors\exceptions-zone.js:9:13)
    at Proxy.<anonymous> (C:\study\nestjs-practice\serverless-nestjs\node_modules\@nestjs\core\nest-factory.js:126:46)
    at Proxy.<anonymous> (C:\study\nestjs-practice\serverless-nestjs\node_modules\@nestjs\core\nest-factory.js:168:54)
    at bootstrapServer (C:\study\nestjs-practice\serverless-nestjs\.build\src\lambda.js:16:17)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async handler (C:\study\nestjs-practice\serverless-nestjs\.build\src\lambda.js:23:20)
    at async InProcessRunner.run (C:\study\nestjs-practice\serverless-nestjs\node_modules\serverless-offline\dist\lambda\handler-runner\in-process-runner\InProcessRunner.js:211:24)
    at async LambdaFunction.runHandler (C:\study\nestjs-practice\serverless-nestjs\node_modules\serverless-offline\dist\lambda\LambdaFunction.js:355:20)
    at async hapiHandler (C:\study\nestjs-practice\serverless-nestjs\node_modules\serverless-offline\dist\events\http\HttpServer.js:601:18)
    at async module.exports.internals.Manager.execute (C:\study\nestjs-practice\serverless-nestjs\node_modules\@hapi\hapi\lib\toolkit.js:45:28)
    at async Object.internals.handler (C:\study\nestjs-practice\serverless-nestjs\node_modules\@hapi\hapi\lib\handler.js:46:20)
    at async exports.execute (C:\study\nestjs-practice\serverless-nestjs\node_modules\@hapi\hapi\lib\handler.js:31:20)
    at async Request._lifecycle (C:\study\nestjs-practice\serverless-nestjs\node_modules\@hapi\hapi\lib\request.js:312:32)
    at async Request._execute (C:\study\nestjs-practice\serverless-nestjs\node_modules\@hapi\hapi\lib\request.js:221:9)

我该怎么修理它?

除了 lambda.ts 和 serverless.yml 文件,其他设置与创建 nest.js 项目时相同。

我会发布代码,请告诉我你是否知道解决方案

lambda.ts

import { NestFactory } from '@nestjs/core';
import { Handler, Context } from 'aws-lambda';
import { ExpressAdapter } from '@nestjs/platform-express';
import * as express from 'express';
import { Server } from 'http';
import { AppModule } from './app.module';
import { eventContext } from 'aws-serverless-express/middleware';
import { createServer, proxy } from 'aws-serverless-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(express),
    );
    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: # The name of the lambda function
    # The module 'handler' is exported in the file 'src/lambda'
    handler: src/lambda.handler
    events:
      - http:
          method: any
          path: /{any+}