$ cnpm install listhen
An elegant HTTP listener.
Install using npm:
npm i 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)
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 on https with SSL enabled.
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.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 --.
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 ????
Copyright 2013 - present © cnpmjs.org | Home |