Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

NestApplication reloading after each GET request #50

Closed
vic1707 opened this issue Jun 29, 2022 · 6 comments
Closed

NestApplication reloading after each GET request #50

vic1707 opened this issue Jun 29, 2022 · 6 comments

Comments

@vic1707
Copy link

vic1707 commented Jun 29, 2022

[this part is due to me forgetting if (import.meta.env.PROD) around the bootstrap function]
to jump to the issue

image

As you can see I get all the init messages twice 馃.

here is the vite.config.ts

import { defineConfig } from 'vite';
import { VitePluginNode } from 'vite-plugin-node';

// eslint-disable-next-line import/no-default-export
export default defineConfig({
  server: {
    port: 3000
  },
  plugins: [
    ...VitePluginNode({
      appName: 'stargraph-api',
      adapter: 'nest',
      appPath: './src/main.ts',
      exportName: 'viteNodeApp',
      tsCompiler: 'swc'
    })
  ],
  optimizeDeps: {
    exclude: [
      '@nestjs/microservices',
      '@nestjs/websockets',
      'cache-manager',
      'class-transformer',
      'class-validator',
      'fastify-swagger'
    ]
  }
});

and my main.ts

import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerCustomOptions, SwaggerModule } from '@nestjs/swagger';

import { ApiModule } from './api.module';

(async () => {
  const app = await NestFactory.create(ApiModule);

  const config = new DocumentBuilder()
    .setTitle('Cats example')
    .setDescription('The cats API description')
    .setVersion('1.0')
    .addTag('cats')
    .build();

  const swaggerCustomOptions: SwaggerCustomOptions = {};

  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('api', app, document, swaggerCustomOptions);

  await app.listen(3000);
})();

export const viteNodeApp = NestFactory.create(ApiModule); // this returns a Promise, which is ok, this plugin can handle it

The rest of the application is the auto generated one

And those are the versions of the packages I used :

    "@nestjs/common": "^8.4.7",
    "@nestjs/core": "^8.4.7",
    "@nestjs/microservices": "^8.4.7",
    "@nestjs/platform-express": "^8.4.7",
    "@nestjs/swagger": "^5.2.1",

    "@swc/core": "^1.2.207",
    "typescript": "^4.7.4",
    "vite": "^2.9.13",
    "vite-plugin-node": "^1.0.0"

Thanks for that awesome plugin, hope I gave enough infos for you to debug ^^

@vic1707
Copy link
Author

vic1707 commented Jun 29, 2022

also sometimes each time I get the base URL I get the log for Nest application successfully started 馃
image

@vic1707
Copy link
Author

vic1707 commented Jun 29, 2022

adding the if (import.meta.env.PROD) because I'm an idiot seems to fix the duplicate instance but doesn't fix the restart(?) of the app after each get
image

@vic1707 vic1707 changed the title NestApplication loading twice ? NestApplication reloading after each GET request Jun 29, 2022
@vic1707
Copy link
Author

vic1707 commented Jun 30, 2022

when I run the production version I still get two instances
image

here's my new main.ts

import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerCustomOptions, SwaggerModule } from '@nestjs/swagger';

import { ApiModule } from './api.module';

if (process.env.NODE_ENV === 'production') {
  (async () => {
    const PORT = process.env.API_PORT || 3000;
    const app = await NestFactory.create(ApiModule);

    const config = new DocumentBuilder()
      .setTitle('Cats example')
      .setDescription('The cats API description')
      .setVersion('1.0')
      .addTag('cats')
      .build();

    const swaggerCustomOptions: SwaggerCustomOptions = {};

    const document = SwaggerModule.createDocument(app, config);
    SwaggerModule.setup('api', app, document, swaggerCustomOptions);

    Logger.log(`Server running on 'localhost:${PORT}'`, 'Bootstrap');
    await app.listen(PORT);
  })();
}

export const viteNodeApp = NestFactory.create(ApiModule);

@sebastiangug
Copy link

I think with the repo here: #58 --- this can be reproduced and tested against as it is merely a symptom of this issue.

@MarioJuniorPro
Copy link

MarioJuniorPro commented Sep 23, 2022

let instance;

export const NestHandler: RequestAdapter<INestApplication> = async ({
  app,
  req,
  res,
}) => {
  if (!instance) {
    await app.init();
    instance = app.getHttpAdapter().getInstance();
  }

  // Todo: handle nest-fastify case

  instance(req, res);
};

@axe-me
Copy link
Owner

axe-me commented Jan 12, 2023

closing along #67 merge

@axe-me axe-me closed this as completed Jan 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants