#node.js #typescript #aws-lambda #nestjs #serverless
Вопрос:
Я учусь без сервера nest.js с помощью aws lambda
Когда я запускаю бессерверный автономный режим и отправляю запрос, произошла ошибка.
[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:studynestjs-practiceserverless-nestjsnode_modules@nestjscoreadaptershttp-adapter.js:20:30)
at NestApplication.use (C:studynestjs-practiceserverless-nestjsnode_modules@nestjscorenest-application.js:140:26)
at C:studynestjs-practiceserverless-nestjsnode_modules@nestjscorenest-factory.js:127:40
at Function.run (C:studynestjs-practiceserverless-nestjsnode_modules@nestjscoreerrorsexceptions-zone.js:9:13)
at Proxy.<anonymous> (C:studynestjs-practiceserverless-nestjsnode_modules@nestjscorenest-factory.js:126:46)
at Proxy.<anonymous> (C:studynestjs-practiceserverless-nestjsnode_modules@nestjscorenest-factory.js:168:54)
at bootstrapServer (C:studynestjs-practiceserverless-nestjs.buildsrclambda.js:16:17)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async handler (C:studynestjs-practiceserverless-nestjs.buildsrclambda.js:23:20)
at async InProcessRunner.run (C:studynestjs-practiceserverless-nestjsnode_modulesserverless-offlinedistlambdahandler-runnerin-process-runnerInProcessRunner.js:211:24)
at async LambdaFunction.runHandler (C:studynestjs-practiceserverless-nestjsnode_modulesserverless-offlinedistlambdaLambdaFunction.js:355:20)
at async hapiHandler (C:studynestjs-practiceserverless-nestjsnode_modulesserverless-offlinedisteventshttpHttpServer.js:601:18)
at async module.exports.internals.Manager.execute (C:studynestjs-practiceserverless-nestjsnode_modules@hapihapilibtoolkit.js:45:28)
at async Object.internals.handler (C:studynestjs-practiceserverless-nestjsnode_modules@hapihapilibhandler.js:46:20)
at async exports.execute (C:studynestjs-practiceserverless-nestjsnode_modules@hapihapilibhandler.js:31:20)
at async Request._lifecycle (C:studynestjs-practiceserverless-nestjsnode_modules@hapihapilibrequest.js:312:32)
at async Request._execute (C:studynestjs-practiceserverless-nestjsnode_modules@hapihapilibrequest.js:221:9)
Как я могу это исправить?
За исключением файлов lambda.ts и serverless.yml, настройки те же, что и при nest.js проект был создан.
Я опубликую код, пожалуйста, дайте мне знать, если вы знаете решение
лямбда.тс
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;
};
без сервера.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 }