
Elegant http listener
βοΈ 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 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'
undefinedFunction signature:undefined
const { url, getURL, server, close } = await listen(handle, options?)
undefinedPlain handle function:undefined
listen('/', ((_req, res) => {
res.end('hi')
})
undefinedWith express/connect:undefined
const express = require('express')
const app = express()
app.use('/', ((_req, res) => {
res.end('hi')
})
listen(app)
portprocess.env.PORT or 3000 or memorized random (see get-port-please)Port to listen.
hostnameprocess.env.HOST || '0.0.0.0'Default hostname to listen.
httpsfalseListen with https protocol. By default uses a self-signed certificated.
certificatePath to https certificate files { key, cert }
selfsignedOptions for self-signed certificate (see selfsigned).
showURLtrue (force disabled on test environment)Show a CLI message for listening URL.
baseURL/openfalse (force disabled on test and production environments)Open URL in browser. Silently ignores errors.
clipboardfalse (force disabled on test and production environments)Copy URL to clipboard. Silently ignores errors.
isTestprocess.env.NODE_ENV === 'test'Detect if running in a test environment to disable some features.
autoClosetrueAutomatically close when an exit signal is received on process.
MIT. Made with π
We use cookies
We use cookies to analyze traffic and improve your experience. You can accept or reject analytics cookies.