为什么 Nestjs + Firebase Cloud Functions 会多次"Starting Nest application"



主要问题

为什么firebase cloud函数上的nestjs必须在firebase serve --only functions初始化一次,并且在第一个API请求时重新初始化一次。

详细信息这是我的main.ts:

import { NestFactory } from '@nestjs/core';
import {
ExpressAdapter,
NestExpressApplication,
} from '@nestjs/platform-express';
import * as express from 'express';
import { HttpsFunction, https, pubsub } from 'firebase-functions';
import { AppModule } from './app/app.module';
const server: express.Express = express();
export const createNestServer = async (expressInstance: express.Express) => {
const adapter = new ExpressAdapter(expressInstance);
const app = await NestFactory.create<NestExpressApplication>(
AppModule,
adapter,
{},
);
app.enableCors();
return app.init();
};
createNestServer(server)
.then((v) => console.log('Nest Ready'))
.catch((err) => console.error('Nest broken', err));
export const api: HttpsFunction = https.onRequest(server);

app/test/test.service.ts:

import { Injectable } from '@nestjs/common';
@Injectable()
export class TestService {
constructor(
) {
console.log(11, 'constructor');
}
async testApi() {
console.log(22, 'gotten api')
return `Hello World!!!`;
}
}

当我firebase serve --only functions时,这里是日志:

> firebase serve --only functions
i  functions: Watching "/Users/jingles/GitHub/pixelhead-backend" for Cloud Functions...
✔  functions[us-central1-api]: http function initialized (http://localhost:5000/test-application/us-central1/api).
>  [Nest] 26036  - 27/04/2022, 04:06:30     LOG [NestFactory] Starting Nest application...
>  11 constructor
>  [Nest] 26036  - 27/04/2022, 04:06:30     LOG [InstanceLoader] AppModule dependencies initialized +41ms
>  [Nest] 26036  - 27/04/2022, 04:06:30     LOG [InstanceLoader] DiscoveryModule dependencies initialized +0ms
>  [Nest] 26036  - 27/04/2022, 04:06:30     LOG [InstanceLoader] TestModule dependencies initialized +0ms
>  [Nest] 26036  - 27/04/2022, 04:06:30     LOG [RoutesResolver] TestController {/test}: +1ms
>  [Nest] 26036  - 27/04/2022, 04:06:30     LOG [RouterExplorer] Mapped {/test, GET} route +0ms
>  [Nest] 26036  - 27/04/2022, 04:06:31     LOG [NestApplication] Nest application successfully started +1033ms
>  Nest Ready

你可以看到这里Starting Nest application,11 constructorNest Ready。所有好。

但是当我请求/test/时,这是我得到的日志:

>  [Nest] 26052  - 27/04/2022, 04:06:53     LOG [NestFactory] Starting Nest application...
i  functions: Beginning execution of "us-central1-api"
>  11 constructor
>  [Nest] 26052  - 27/04/2022, 04:06:53     LOG [InstanceLoader] AppModule dependencies initialized +44ms
>  [Nest] 26052  - 27/04/2022, 04:06:53     LOG [InstanceLoader] DiscoveryModule dependencies initialized +1ms
>  [Nest] 26036  - 27/04/2022, 04:06:30     LOG [InstanceLoader] TestModule dependencies initialized +0ms
>  [Nest] 26036  - 27/04/2022, 04:06:30     LOG [RoutesResolver] TestController {/test}: +1ms
>  [Nest] 26036  - 27/04/2022, 04:06:30     LOG [RouterExplorer] Mapped {/test, GET} route +0ms
>  22 gotten api
i  functions: Finished "us-central1-api" in ~1s
>  [Nest] 26052  - 27/04/2022, 04:06:54     LOG [NestApplication] Nest application successfully started +985ms
>  Nest Ready

问题在这里你可以看到,它再次Starting Nest applicationNest Ready22 gotten api之后。

有什么问题吗?

我想你有两个不同的问题:

  1. 已"启动嵌套应用程序";2次:Firebase函数是一个无服务器环境,这意味着您无法保证云中的哪个实例将为您的请求提供服务:已初始化或未初始化。仿真器只是简单地模拟"冷启动"。在本例中,在"/test/"请求到来,这意味着索引代码被执行(env boosed),然后请求被服务。

  2. "Nest Ready"在22个已获得的api"这主要是由于这两行代码:

createNestServer(server)
.then((v) => console.log('Nest Ready'))
.catch((err) => console.error('Nest broken', err));
export const api: HttpsFunction = https.onRequest(server);

第一行初始化Express实例上的Nest应用,但不阻止第二行为到达"/test/"的请求提供服务。端点。这款应用可能只有"一半"。当请求到达代码时初始化,所以你不会得到任何错误,请求无论如何都会被服务,但通常你应该组织你的代码,首先引导Nest应用程序,然后服务请求。

重要提示:由于我们正在讨论无服务器环境,每次请求进来时,您可能处于Nest应用程序已经初始化或未初始化的情况。然后你应该相应地组织你的代码。

查看此处参考:https://docs.nestjs.com/faq/serverless

相关内容

  • 没有找到相关文章

最新更新