Skip to content

Instantly share code, notes, and snippets.

@drewwiens
Last active January 14, 2022 19:12
Show Gist options
  • Save drewwiens/50c1fdeda9cad0ab9da0f091a6d3b99e to your computer and use it in GitHub Desktop.
Save drewwiens/50c1fdeda9cad0ab9da0f091a6d3b99e to your computer and use it in GitHub Desktop.
Serve a NestJS application from a sub-path using ExpressJS sub-app feature
import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { ExpressAdapter } from '@nestjs/platform-express';
import { urlencoded, json } from 'express';
import { AppModule } from './app/app.module';
(async () => {
const nestExpressAppAdapter = new ExpressAdapter();
const app = await NestFactory.create(AppModule, nestExpressAppAdapter, {});
// Serve the NestJS app as a sub-app at the base href path:
const expressAppAdapter = new ExpressAdapter();
const expressApp = expressAppAdapter.getInstance();
const nestExpressApp = nestExpressAppAdapter.getInstance();
const baseHref = process.env.BASE_HREF || '/';
expressApp.use(baseHref, nestExpressApp);
await app.init();
expressApp.listen(appPort, () => {
console.log(`Listening on port ${appPort}`);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment