import { serve } from "https://deno.land/std/http/server.ts";
import { getPort, portRange, portSelection } from "https://deno.land/x/port/mod.ts";
const s = serve({ port: portSelection([3011, 8080], { log: true }) });
for await (const req of s) {
req.respond({ body: "Response from server\n" });
}
The module works with different frameworks of Deno. You can check an example with OAK in the examples/
folder. It is inspired by get-port, find-free-port or other related packages for nodejs.
getPort(port?: number, options?: { log?: boolean }): number
If port was specified it returns that port
, if it was not, it returns a random port assigned from the Operating System.
Optional parameters:
{ port?: number }
{ log?: boolean }
Example usage:
getPort(3000, { log: true });
getPort(3000); // by default log is false
portSelection(ports: Array<number>, options?: { log?: boolean}): number
Returns a port from the specified selection of ports in the ports
array.
Required parameters:
ports: Array<numbers>
Optional parameters:
{ log?: boolean }
Example usage:
portSelection([8080, 3001], { log: true });
portSelection([3000, 3011]); // by default log is false
portRange(min_port: number, max_port: number, options?: { log?: boolean}): number
Returns a port in between min_port
and max_port
.
Required parameters:
min_port: number
max_port: number
Optional parameters:
{ log?: boolean }
Example usage:
portRange(3001, 8080, { log: true });
portRange(3000, 3011); // by default log is false
There are no race conditions when the ports are assigned.