Git Product home page Git Product logo

deno-port's Introduction

deno-port

Build Status

Get an available TCP port for your Deno server

Basic Usage

  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.


API

getPort()

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:

  1. { port?: number }
  2. { log?: boolean }

Example usage:

getPort(3000, { log: true }); 

getPort(3000); // by default log is false

portSelection([1024, 1025, ..., n])

portSelection(ports: Array<number>, options?: { log?: boolean}): number

Returns a port from the specified selection of ports in the ports array.

Required parameters:

  1. ports: Array<numbers>

Optional parameters:

  1. { log?: boolean }

Example usage:

portSelection([8080, 3001], { log: true }); 

portSelection([3000, 3011]); // by default log is false

portRange(min_port, max_port)

portRange(min_port: number, max_port: number, options?: { log?: boolean}): number

Returns a port in between min_port and max_port.

Required parameters:

  1. min_port: number
  2. max_port: number

Optional parameters:

  1. { log?: boolean }

Example usage:

portRange(3001, 8080, { log: true }); 

portRange(3000, 3011); // by default log is false

Other

There are no race conditions when the ports are assigned.

deno-port's People

Contributors

adoi avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

jdx

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.