diff --git a/ext/node/polyfills/http.ts b/ext/node/polyfills/http.ts index b1a536113d..40155d998c 100644 --- a/ext/node/polyfills/http.ts +++ b/ext/node/polyfills/http.ts @@ -1657,7 +1657,7 @@ export class ServerImpl extends EventEmitter { #httpConnections: Set = new Set(); #listener?: Deno.Listener; - #addr: Deno.NetAddr; + #addr: Deno.NetAddr | null = null; #hasClosed = false; #server: Deno.HttpServer; #unref = false; @@ -1843,6 +1843,7 @@ export class ServerImpl extends EventEmitter { } address() { + if (this.#addr === null) return null; return { port: this.#addr.port, address: this.#addr.hostname, diff --git a/tests/unit_node/http_test.ts b/tests/unit_node/http_test.ts index aeb92f129d..0935aeac03 100644 --- a/tests/unit_node/http_test.ts +++ b/tests/unit_node/http_test.ts @@ -1251,3 +1251,8 @@ Deno.test("[node/http] http.request() post streaming body works", async () => { clearTimeout(timeout); assertEquals(server.listening, false); }); + +Deno.test("[node/http] Server.address() can be null", () => { + const server = http.createServer((_req, res) => res.end("it works")); + assertEquals(server.address(), null); +});