Last active
January 14, 2022 19:12
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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