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'
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)
- Default:
process.env.PORT
or 3000 or memorized random (see get-port-please)
Port to listen.
- Default:
false
Listen with https
protocol. By default uses a self-signed certificated.
Path to https certificate files { key, cert }
Options for self-signed certificate (see selfsigned).
- Default:
true
(force disabled on test environment)
Show a CLI message for listening URL.
- Default:
/
- Default:
false
(force disabled on test and production environments)
Open URL in browser. Silently ignores errors.
- Default:
false
(force disabled on test and production environments)
Copy URL to clipboard. Silently ignores errors.
- Default:
process.env.NODE_ENV === 'test'
Detect if running in a test environment to disable some features.
- Default:
true
Automatically close when an exit signal is received on process.
MIT. Made with ๐