create-listening-server
v2.1.0
Published
Promise-based API to create listening `http.Server` instances.
Downloads
5,869
Readme
create-listening-server
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