2.1.0 • Published 1 year ago

create-listening-server v2.1.0

Weekly downloads
6,411
License
MIT
Repository
github
Last release
1 year ago

create-listening-server

npm version Build Status

Promise-based API to create listening http.Server instances.

Safe creation functionality is also exposed, with automated consecutive port retries when "address in use" errors occur. Useful for tests and development mode.

Getting started

Install the library in an existing project:

npm i create-listening-server

Example usage

Creation of an http.Server with a specific port:

import { createListeningHttpServer } from "create-listening-server";
import express from "express";

const PORT = 3000;

async function main() {
  const app = express();
  app.use(/* middleware */);

  const httpServer = await createListeningHttpServer(PORT, app);
}

Creation of an http.Server with automated consecutive ports retries:

import { safeListeningHttpServer } from "create-listening-server";
import express from "express";

const PREFERRED_PORT = 3000;

async function main() {
  const app = express();
  app.use(/* middleware */);

  const { httpServer, port } = await safeListeningHttpServer(PREFERRED_PORT, app);
}

License

MIT