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 render some routes using Nest and some using Vite? #95

Open
thgh opened this issue Jun 19, 2023 · 1 comment
Open

How to render some routes using Nest and some using Vite? #95

thgh opened this issue Jun 19, 2023 · 1 comment

Comments

@thgh
Copy link

thgh commented Jun 19, 2023

I have a Nest js application that routes some static assets to a create-react-app server. I would like to combine them and have everything run under Vite. The Nestjs example only renders the Nestjs application, it does not serve the js/html that was served before adding the plugin.

Is that possible?

@cainrus
Copy link

cainrus commented Jan 3, 2024

@thgh Add namespace for nest server. I hope that helps.

// ./server/bootstrap.ts
export async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.setGlobalPrefix('api');
  return app;
}

Setup custom adapter

// vite.config.ts
import once from 'lodash/once';
import type { NestApplication } from '@nestjs/core';

const bootstrapOnce = once(
  async <T extends NestApplication>(init: () => Promise<T>): Promise<T> => {
    const app = await init();
    await app.init();
    return app;
  },
);

// ...
tsconfigPaths(),
    ...VitePluginNode({
      async adapter({ app, server, req, res, next }) {
        if (req.url.startsWith('/api/')) {
          bootstrapOnce(app).then((app: NestApplication) => {
            const instance = app.getHttpAdapter().getInstance();
            instance(req, res);
          });
        } else {
          next();
        }
      },
      appPath: './server/bootstrap.ts',
      tsCompiler: 'swc',
      exportName: 'bootstrap',
    }),

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

2 participants