Skip to content

Commit ba76ecf

Browse files
committed
feat(http-server): exposes underlying http server before start
To create a sockio server from the HttpServer, we need to access the underlying http/https server.
1 parent f89d6ae commit ba76ecf

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

packages/http-server/src/http-server.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class HttpServer {
7575
private _protocol: HttpProtocol;
7676
private _address: AddressInfo;
7777
private requestListener: RequestListener;
78-
private server: http.Server | https.Server;
78+
readonly server: http.Server | https.Server;
7979
private serverOptions?: HttpServerOptions;
8080

8181
/**
@@ -91,12 +91,6 @@ export class HttpServer {
9191
this._port = serverOptions ? serverOptions.port || 0 : 0;
9292
this._host = serverOptions ? serverOptions.host : undefined;
9393
this._protocol = serverOptions ? serverOptions.protocol || 'http' : 'http';
94-
}
95-
96-
/**
97-
* Starts the HTTP / HTTPS server
98-
*/
99-
public async start() {
10094
if (this._protocol === 'https') {
10195
this.server = https.createServer(
10296
this.serverOptions as https.ServerOptions,
@@ -105,6 +99,12 @@ export class HttpServer {
10599
} else {
106100
this.server = http.createServer(this.requestListener);
107101
}
102+
}
103+
104+
/**
105+
* Starts the HTTP / HTTPS server
106+
*/
107+
public async start() {
108108
this.server.listen(this._port, this._host);
109109
await pEvent(this.server, 'listening');
110110
this._listening = true;

packages/http-server/test/integration/http-server.integration.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
givenHttpServerConfig,
1313
} from '@loopback/testlab';
1414
import * as makeRequest from 'request-promise-native';
15-
import {IncomingMessage, ServerResponse} from 'http';
15+
import {IncomingMessage, ServerResponse, Server} from 'http';
1616
import * as path from 'path';
1717
import * as fs from 'fs';
1818

@@ -121,6 +121,11 @@ describe('HttpServer (integration)', () => {
121121
.which.is.an.Object();
122122
});
123123

124+
it('exports server before start', async () => {
125+
server = new HttpServer(dummyRequestHandler);
126+
expect(server.server).to.be.instanceOf(Server);
127+
});
128+
124129
it('resets address when server is stopped', async () => {
125130
server = new HttpServer(dummyRequestHandler);
126131
await server.start();

0 commit comments

Comments
 (0)