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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to extra config to app #25

Closed
wangfengyuan opened this issue Dec 10, 2021 · 3 comments
Closed

how to extra config to app #25

wangfengyuan opened this issue Dec 10, 2021 · 3 comments

Comments

@wangfengyuan
Copy link

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

if (process.env.NODE_ENV === 'production') {
  async function bootstrap() {
    const app = await NestFactory.create(AppModule);


   // here is my extra operation about app,
    app.setGlobalPrefix('/base/');
    app.set('trust proxy', true);
    app.disable('x-powered-by');
   // ....

    await app.listen(3000);
  }

  bootstrap();
}

export const viteNodeApp = NestFactory.create(AppModule);

after that, my extra config about app did not work,i want to know how to organize my code

@axe-me
Copy link
Owner

axe-me commented Feb 10, 2022

hi @wangfengyuan sorry for late reply. To achieve this, I prob should add in some sorta hook to the plugin.

For now you could create a customize adapter to put in your configs:

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

export default defineConfig({
  plugins: [
    ...VitePluginNode({
      adapter: async function (app, req, res) {
       	await app.init();
		// your configs:
  		app.setGlobalPrefix('/api');
	    app.set('trust proxy', true);
	    app.disable('x-powered-by');

	    const instance = app.getHttpAdapter().getInstance();
	  
	    instance(req, res);
      },
      appPath: './app.ts'
    })
  ]
});

let me know if this works for you

@axe-me axe-me closed this as completed Feb 10, 2022
@yusufkandemir
Copy link

IMO, this is approach is more understandable and cleaner:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function createNestApp() {
  const app = await NestFactory.create(AppModule);

  // Extra configuration
  app.setGlobalPrefix('/base/');
  app.set('trust proxy', true);
  app.disable('x-powered-by');
  // ....

  return app;
}

if (process.env.NODE_ENV === 'production') {
  createNestApp().then(async (app) => {
    await app.listen(3000);
  });
}

export const viteNodeApp = createNestApp();

I think updating the documentation with some examples would be a simpler solution compared to providing extra hooks. It would also be more flexible and can help keep the maintenance costs lower.

@wangfengyuan
Copy link
Author

@yusufkandemir yes it works, thanks for your suggestion

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

3 participants