Skip to content

Commit 9ab38e5

Browse files
committed
add method to resume listening
1 parent 3927630 commit 9ab38e5

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/Server.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Server extends EventEmitter<Server.Events> {
2525
*/
2626
public readonly errors = new ServerErrorRegistry();
2727
private readonly server: http.Server;
28+
private readonly port?: number;
2829
private readonly copyOrigin: boolean;
2930
private readonly handleConditionalRequests: boolean;
3031

@@ -42,10 +43,11 @@ class Server extends EventEmitter<Server.Events> {
4243
if (!this.globalHeaders.has("server"))
4344
this.globalHeaders.set("Server", `cldn/${packageJson.version}`);
4445

46+
this.port = options.port;
4547
this.copyOrigin = options.copyOrigin ?? false;
4648
this.handleConditionalRequests = options.handleConditionalRequests ?? true;
4749

48-
this.server.listen(options.port, process.env.HOST, () => this.emit("listening"));
50+
if (this.port !== undefined) this.listen(this.port);
4951

5052
this.once("listening", () => {
5153
if (this.listenerCount("error") === 0)
@@ -82,6 +84,15 @@ class Server extends EventEmitter<Server.Events> {
8284
this.emit("closed");
8385
}
8486

87+
/**
88+
* Start listening for connections.
89+
*/
90+
public listen(port: number) {
91+
if (this.server.listening)
92+
throw new Error("Server is already listening.");
93+
this.server.listen(port, process.env.HOST, () => this.emit("listening"));
94+
}
95+
8596
private async listener(req: http.IncomingMessage, res: http.ServerResponse) {
8697
let apiRequest: Request;
8798
try {

0 commit comments

Comments
 (0)