Skip to content

Latest commit

History

History
168 lines (99 loc) 路 3.74 KB

README.md

File metadata and controls

168 lines (99 loc) 路 3.74 KB

listhen

馃憘 listhen

Elegant http listener

npm version npm downloads Github Actions Codecov

鉁旓笍 Promisified interface for listening and closing server

鉁旓笍 Works with express/connect or plain http handle function

鉁旓笍 Support HTTP and HTTPS

鉁旓笍 Automatically assign a port or fallback to human friendly alternative (with get-port-please)

鉁旓笍 Automatically generate listening URL and show on console

鉁旓笍 Automatically copy URL to clipboard

鉁旓笍 Automatically open in browser (opt-in)

鉁旓笍 Automatically generate self-signed certificate

鉁旓笍 Automatically detect test and production environments

鉁旓笍 Automatically close on exit signal

鉁旓笍 Gracefully shutdown server with http-shutdown

Install

Install using npm or yarn:

npm i listhen
# or
yarn add listhen

Import into your Node.js project:

// CommonJS
const { listen } = require('listhen')

// ESM
import { listen } from 'listhen'

Usage

Function signature:

const { url, getURL, server, close } = await listen(handle, options?)

Plain handle function:

listen((_req, res) => {
  res.end('hi')
})

With express/connect:

const express = require('express')
const app = express()

app.use('/', ((_req, res) => {
  res.end('hi')
})

listen(app)

Options

port

  • Default: process.env.PORT or 3000 or memorized random (see get-port-please)

Port to listen.

hostname

  • Default: process.env.HOST || '0.0.0.0'

Default hostname to listen.

https

  • Type: Boolean | Object
  • Default: false

Listen on https with SSL enabled.

Self Signed Certificate

By setting https: true, listhen will use an auto generated self-signed certificate.

You can set https to an object for custom options. Possible options:

  • domains: (Array) Default is ['localhost', '127.0.0.1', '::1'].
  • validityDays: (Number) Default is 1.

User Provided Certificate

Set https: { cert, key } where cert and key are path to the ssl certificates.

You can also provide inline cert and key instead of reading from filesystem. In this case, they should start with --.

showURL

  • Default: true (force disabled on test environment)

Show a CLI message for listening URL.

baseURL

  • Default: /

open

  • Default: false (force disabled on test and production environments)

Open URL in browser. Silently ignores errors.

clipboard

  • Default: false (force disabled on test and production environments)

Copy URL to clipboard. Silently ignores errors.

isTest

  • Default: process.env.NODE_ENV === 'test'

Detect if running in a test environment to disable some features.

autoClose

  • Default: true

Automatically close when an exit signal is received on process.

License

MIT. Made with 馃挅