question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

http.Server doesn't expose randomly selected port

See original GitHub issue

If you specify port 0, as serve({ port: 0 }) the library works correctly and automatically selects a port. However, the port is accessible to the process cannot advertise where it is listening on. In some cases it is desirable to start a process on any available port, and then advertise it, be it through the console, or some other transport/file.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
AaronOcommented, Jul 15, 2021

@aricart @getspooky You can actually already do this, though it’s somewhat quirky (.port and Addr typing), see https://github.com/denoland/deno/pull/11410#issuecomment-880747416:

import { serve } from "https://deno.land/std@0.101.0/http/server.ts";
const server = serve({ port: 0 });
const serverAddr = server.listener.addr as Deno.NetAddr;
console.log(`http://localhost:${serverAddr.port}`);
for await (const req of server) {
  req.respond({ body: "Hello World\n" });
}
0reactions
kt3kcommented, Sep 28, 2022

@iuioiua Thanks. Right, this was resolved in #2311. Now the listening port is available in the option of onListen handler.

Read more comments on GitHub >

github_iconTop Results From Across the Web

server using port 0, can't access to the random port #5102
but when I tried to access to the port with server.listener.addr.port , I had this message: error TS2339: Property 'port' does not exist...
Read more >
Expose random port to docker-compose.yml
I can communicate with container within docker-compose file using fixed port for container and exposing them for host machine. server. port=0 ...
Read more >
How To Expose or Publish Docker Ports
Docker identifies all ports exposed using the EXPOSE directive and those exposed using the –expose parameter. Then, each exposed port is mapped ...
Read more >
How to automatically choose a free port in ASP.NET Core 3.0
For example, to bind to a random http and https port on the loopback (localhost) address, run your application using the following command:...
Read more >
Registered Port - an overview
Random port numbers (sometimes called ephemeral port numbers) have values greater than 1024, which are assigned arbitrarily using TCP or UDP when the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found