Skip to content

Commit

Permalink
feat(http-server): exposes underlying http server before start
Browse files Browse the repository at this point in the history
To create a sockio server from the HttpServer, we need to access the
underlying http/https server.
  • Loading branch information
raymondfeng committed Nov 21, 2018
1 parent f89d6ae commit ba76ecf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
14 changes: 7 additions & 7 deletions packages/http-server/src/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class HttpServer {
private _protocol: HttpProtocol;
private _address: AddressInfo;
private requestListener: RequestListener;
private server: http.Server | https.Server;
readonly server: http.Server | https.Server;
private serverOptions?: HttpServerOptions;

/**
Expand All @@ -91,12 +91,6 @@ export class HttpServer {
this._port = serverOptions ? serverOptions.port || 0 : 0;
this._host = serverOptions ? serverOptions.host : undefined;
this._protocol = serverOptions ? serverOptions.protocol || 'http' : 'http';
}

/**
* Starts the HTTP / HTTPS server
*/
public async start() {
if (this._protocol === 'https') {
this.server = https.createServer(
this.serverOptions as https.ServerOptions,
Expand All @@ -105,6 +99,12 @@ export class HttpServer {
} else {
this.server = http.createServer(this.requestListener);
}
}

/**
* Starts the HTTP / HTTPS server
*/
public async start() {
this.server.listen(this._port, this._host);
await pEvent(this.server, 'listening');
this._listening = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
givenHttpServerConfig,
} from '@loopback/testlab';
import * as makeRequest from 'request-promise-native';
import {IncomingMessage, ServerResponse} from 'http';
import {IncomingMessage, ServerResponse, Server} from 'http';
import * as path from 'path';
import * as fs from 'fs';

Expand Down Expand Up @@ -121,6 +121,11 @@ describe('HttpServer (integration)', () => {
.which.is.an.Object();
});

it('exports server before start', async () => {
server = new HttpServer(dummyRequestHandler);
expect(server.server).to.be.instanceOf(Server);
});

it('resets address when server is stopped', async () => {
server = new HttpServer(dummyRequestHandler);
await server.start();
Expand Down

0 comments on commit ba76ecf

Please sign in to comment.