我正在研究无服务器的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。
我会附上代码,如果您有任何问题,请告诉我。
λ.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;
};
无服务器.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+}