/create-listening-server

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

Primary LanguageTypeScriptMIT LicenseMIT

create-listening-server

Build Status npm version

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:

yarn add 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