Skip to content

Commit 2f83052

Browse files
author
kamil.mysliwiec
committed
fix(@nestjs/core, @nestjs/common) pass server to listen method, add configuration class, multiple servers, global route prefix (nestjs#70, nestjs#20, nestjs#40)
1 parent 37ea6c3 commit 2f83052

4 files changed

Lines changed: 40 additions & 7 deletions

File tree

example/server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { ApplicationModule } from './modules/app.module';
44
const port = 3001;
55
const app = NestFactory.create(ApplicationModule);
66

7-
app.listen(port, () => {
7+
app.init();
8+
app.listen(port, (server) => {
89
console.log('Application listen on port:', port);
10+
server.close();
911
process.exit();
1012
});
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export interface INestApplication {
2-
listen(port: number, callback?: () => void);
2+
init(): void;
3+
listen(port: number, callback?: (server?) => void): void;
4+
setGlobalPrefix(prefix: string): void;
35
}

src/core/application-config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export class ApplicationConfig {
2+
private globalPrefix = '';
3+
4+
public setGlobalPrefix(prefix: string) {
5+
this.globalPrefix = prefix;
6+
}
7+
8+
public getGlobalPrefix() {
9+
return this.globalPrefix;
10+
}
11+
}

src/core/nest-application.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ import { messages } from './constants';
88
import { MicroservicesModule } from '@nestjs/microservices/microservices-module';
99
import { Resolver } from './router/interfaces/resolver.interface';
1010
import { INestApplication } from '@nestjs/common';
11+
import { ApplicationConfig } from './application-config';
12+
import { validatePath } from '@nestjs/common/utils/shared.utils';
1113

1214
export class NestApplication implements INestApplication {
13-
private readonly routesResolver: Resolver;
15+
private readonly config = new ApplicationConfig();
1416
private readonly logger = new Logger(NestApplication.name);
17+
private readonly routesResolver: Resolver;
1518

1619
constructor(
1720
private readonly container: NestContainer,
@@ -26,12 +29,27 @@ export class NestApplication implements INestApplication {
2629
MicroservicesModule.setupClients(this.container);
2730
}
2831

29-
public listen(port: number, callback?: () => void) {
30-
this.setupMiddlewares(this.express);
31-
this.setupRoutes(this.express);
32+
public init() {
33+
const router = ExpressAdapter.createRouter();
34+
this.setupMiddlewares(router);
35+
this.setupRoutes(router);
36+
37+
this.express.use(
38+
validatePath(this.config.getGlobalPrefix()),
39+
router,
40+
);
3241

3342
this.logger.log(messages.APPLICATION_READY);
34-
return this.express.listen(port, callback);
43+
}
44+
45+
public listen(port: number, callback?: () => void) {
46+
const server = this.express.listen(port, () => {
47+
callback && callback.call(callback, server);
48+
});
49+
}
50+
51+
public setGlobalPrefix(prefix: string) {
52+
this.config.setGlobalPrefix(prefix);
3553
}
3654

3755
private setupMiddlewares(instance) {

0 commit comments

Comments
 (0)