NestApplication reloading after each GET request
[this part is due to me forgetting if (import.meta.env.PROD) around the bootstrap function]
to jump to the issue
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 ^^
also sometimes each time I get the base URL I get the log for Nest application successfully started 🤔

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

when I run the production version I still get two instances

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);
I think with the repo here: https://github.com/axe-me/vite-plugin-node/issues/58 --- this can be reproduced and tested against as it is merely a symptom of this issue.
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);
};
closing along #67 merge