Détail du package

listhen

unjs4.5mMIT1.9.0

👂 Elegant HTTP Listener

readme

👂 listhen

npm version npm downloads

Elegant HTTP listener!

👉 Online Playground

Features

✅ Dev server with HMR, static, WebSockets and TypeScript support with unjs/jiti

✅ Works with Node.js, Express, and unjs/h3 out of the box

✅ Show the QR code of the public URL with unjs/uqr

✅ Tunnel your local server to the world with unjs/untun

✅ Assign a port or fallback to a nicer alternative with unjs/get-port-please

✅ Gracefully shutdown Server with http-shutdown

✅ Zero Config WebSockets with unjs/crossws

✅ Copy the URL to the clipboard

✅ HTTPS support with self-signed certificates

✅ Open URL in browser

✅ Detect test and production environments to auto-adjust behavior

✅ Close on the exit signal

Quick Usage (CLI)

You can run your applications in localhost with TypeScript support and watch mode using listhen CLI:

Create index.ts:

export default (req, res) => {
  res.end("Hello World!");
};

or using unjs/h3:

import { createApp, eventHandler } from "h3";

export const app = createApp();

app.use(
  "/",
  eventHandler(() => "Hello world!"),
);

or use npx to invoke listhen command:

npx listhen -w ./index.ts

Usage (API)

Install package:

# pnpm
pnpm i listhen

# npm
npm i listhen

# yarn
yarn add listhen

Import into your Node.js project:

// CommonJS
const { listen, listenAndWatch } = require("listhen");

// ESM
import { listen, listenAndWatch } from "listhen";
const handler = (req, res) => {
  res.end("Hi!")
}

// listener: { url, getURL, server, close, ... }
const listener = await listen(handler, options)

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 the cert and key are paths to the SSL certificates. With an encrypted private key, you also need to set passphrase on the https object.

To provide a certificate stored in a keystore set https: { pfx } with a path to the keystore. When the keystore is password protected also set passphrase.

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

showURL

  • Default: true (force disabled on a test environment)

Show a CLI message for the listening URL.

baseURL

  • Default: /

open

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

Open the URL in the browser. Silently ignores errors.

clipboard

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

Copy the URL to the 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 event, SIGTERM, SIGINT or SIGHUP signal is received in the process.

publicURL

  • Default: (the first public URL listening)

The public URL to show in the CLI output

qr

  • Default: true

Print QR Code for public address.

public

  • Default: false for development or when hostname is localhost and true for production

When enabled, listhen tries to listen to all network interfaces. You can also enable this option using --host CLI flag.

ws

  • Default: false

Enable experimental WebSocket support using unjs/crossws or node upgrade handler.

Option can be a function for Node.js upgrade handler ((req, head) => void) or an Object to use CrossWS Hooks.

When using dev server CLI, you can easily use --ws and a named export called websocket to define CrossWS Hooks with HMR support!

License

Published under the MIT license. Made by @pi0 and community 💛


🤖 auto updated with automd

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

v1.9.0

compare changes

🚀 Enhancements

  • Update crossws to 0.3.x (#181)

📦 Build

  • Relax crossws dep constraint (bcb9bf4)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.8.0

compare changes

🚀 Enhancements

🩹 Fixes

  • dev: Call dynamic websocket resolver (7360d27)
  • dev: Normalize paths with pathe (#166)

📖 Documentation

🏡 Chore

🤖 CI

❤️ Contributors

v1.7.2

compare changes

🩹 Fixes

  • dev: Call dynamic websocket resolver (7360d27)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.7.1

compare changes

💅 Refactors

  • Update to crossws 0.2 api (b29607f)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.7.0

compare changes

🚀 Enhancements

  • ws: Support dynamic websocket resolver (7b006e2)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.6.0

compare changes

🚀 Enhancements

  • Experimental websocket support (#158)

🏡 Chore

  • Update lockfile (ebef2d1)
  • Remove ws from shared args for now (d115045)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.5.6

compare changes

🩹 Fixes

  • Apply default localhost in internal generateURL util (6c76d31)
  • dev: Fix absolute paths imports on windows (#142)

📖 Documentation

  • Update h3 example (#147)

🏡 Chore

❤️ Contributors

v1.5.5

compare changes

🩹 Fixes

  • Apply default localhost in internal generateURL util (6c76d31)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.5.4

compare changes

🩹 Fixes

  • Use localhost when host is empty for getURL (9ec7d77)

📖 Documentation

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)
  • Tech Genius

v1.5.3

compare changes

🩹 Fixes

  • Correct private host warning message (df5ff4d)
  • Prefer explicit hostname option over HOST env (0674d96)

🏡 Chore

  • release: V1.5.2 (1aab0dd)
  • Revert to codecov/codecov-action@v3 (c911a1f)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.5.2

compare changes

🔥 Performance

  • Imprve default host for windows, docker and wsl (#126)

💅 Refactors

  • Use std-env for stackblitz detection (933c0c3)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.5.1

compare changes

🩹 Fixes

  • Open browser on wsl (#124)

🏡 Chore

❤️ Contributors

v1.5.0

compare changes

🚀 Enhancements

  • Trap SIGINT, SIGTERM and SIGHUP for autoclosing (#108)

🩹 Fixes

  • Allow valid ipv6 as hostnames (78dd4b7)

🏡 Chore

❤️ Contributors

v1.4.8

compare changes

🩹 Fixes

  • Type and provide listener.address (972fac7)

🏡 Chore

  • Update get-port-please and jiti (8be655c)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.4.7

compare changes

🩹 Fixes

  • Validate hostname after applying defaults (e6c1b89)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.4.6

compare changes

🩹 Fixes

  • Validate input hostname and warn (bfc8149)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.4.5

compare changes

🩹 Fixes

  • Update get-port-please and pass public flag (3de5b10)
  • cli: Remove default: false for opt-in flag (180c685)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.4.4

compare changes

🩹 Fixes

  • Use ipv4 for windows and wsl default hostname (#119)

💅 Refactors

  • Tidy up open, wsl, and docker utils (#118)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.4.3

compare changes

🩹 Fixes

  • Prevent double brackets for ipv6 format (2110e40)

🏡 Chore

  • Update playground version (f9f1be3)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.4.2

compare changes

🩹 Fixes

  • Better options handling for public host (#112)
  • cli: Handle --host with boolean value (#113)
  • Several bugfixes for generated urls (#114)

💅 Refactors

  • Show qr code above listening urls (#115)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.4.1

compare changes

🩹 Fixes

  • dev: Update @parcel/watcher-wasm usage to 2.3.0 (329230a)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.4.0

compare changes

🚀 Enhancements

  • cli: Expose getArgs and parseArgs (#109)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.3.1

compare changes

🩹 Fixes

  • Update qr code padding (#105)
  • Use unicode char for network placeholder (e80f658)

🏡 Chore

❤️ Contributors

v1.3.0

compare changes

🚀 Enhancements

  • Print qr code for public address (#89)
  • qr and publicURL options and --qr, --no-qr, and --public-url cli flags (#97)
  • public option and --host flag to disable network expose in development by default (#98)
  • Stackblitz auto url detection (experimental) (#99)
  • cli: Accept https.* options (#93)
  • Tunnel support via unjs/untun and cloudflared (experimental) (#102)

🔥 Performance

  • dev: Use @parcel/watcher whenever available (#95)

🩹 Fixes

  • dev: Try to resolve h3 from user node_modules first (#94)
  • dev: Show better error for when no compatible handler is exported (#92)

💅 Refactors

  • Better watcher warning message (ec92cf4)
  • Fix option types and handling (ad82a70)
  • Use consistent emojies (4b26902)
  • Simplify console output (e741be0)

🏡 Chore

✅ Tests

  • Fix order mismatch (#90)
  • setup: Extend certificate generation to also write ca.pem, ca-key.pem and certificate chain to files (#103)

❤️ Contributors

v1.2.2

compare changes

🩹 Fixes

  • watcher: Use @parcel/watcher-wasm for non node.js environment compatibility (#85)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.2.1

compare changes

🩹 Fixes

  • Avoid directly importing availableParallelism from os (bdd8771)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.2.0

compare changes

🚀 Enhancements

  • Improve watcher experience (#82)
  • Dev server with serve static support (#83)
  • https: Support using encrypted private key and pkcs12 (pfx) keystore (#80)

🩹 Fixes

  • watcher: Resolve relative paths without leading dot (c8388f1)

🏡 Chore

🤖 CI

  • Use conventional commit for autofix action (#79)

❤️ Contributors

v1.1.2

compare changes

📦 Build

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.1.1

compare changes

📦 Build

  • Include bin in npm dist files (03e66b0)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.1.0

compare changes

🚀 Enhancements

  • Support basic listenAndWatch (#74)
  • listhen cli (#75)

💅 Refactors

📦 Build

  • Remove non existing cjs dist from exports (#69)
  • Move types field to the first (5abd82c)
  • Fix cjs compatibility (65d667c)

🏡 Chore

❤️ Contributors

v1.0.4

compare changes

🩹 Fixes

  • Add default port range (#63)

📖 Documentation

  • Update badges and content (b7cf9ad)

🏡 Chore

❤️ Contributors

v1.0.3

compare changes

🏡 Chore

  • Update ufo and other dependencies (d3db211)

❤️ Contributors

v1.0.2

compare changes

🏡 Chore

  • Upgrade deps (#55)
  • Update deps (18d7542)
  • Switch to changelogen for release (7c1bf04)

🎨 Styles

❤️ Contributors

1.0.1 (2022-11-28)

Bug Fixes

  • pass port options object to getPort (#47) (8cc07e0)

1.0.0 (2022-11-15)

0.3.5 (2022-11-02)

Bug Fixes

  • respect hostname when opening url (#40) (d1e985d)

0.3.4 (2022-09-20)

Bug Fixes

  • add custom hostname to alternative domains (d7ffa58)
  • fix type for https only for listener instead of options (59a2c76)

0.3.3 (2022-09-20)

Bug Fixes

  • narrow down listener.https type (c8279e2)

0.3.2 (2022-09-20)

Features

  • expose resolve https options (84023f4)

Bug Fixes

  • display provided hostname (ee1df6d)

0.3.1 (2022-09-15)

Features

0.3.0 (2022-09-15)

⚠ BREAKING CHANGES

  • certificate and selfsigned options are merged with https

Features

0.2.15 (2022-08-08)

Bug Fixes

  • deps: update get-port please (d0267d8)

0.2.14 (2022-08-08)

Features

  • update ger-port-please with verbose error (8e6f023)

Bug Fixes

  • pass hostname to getPort as well (#34) (0ced0a4)

0.2.13 (2022-06-15)

Bug Fixes

0.2.12 (2022-06-13)

Features

  • option to show baseURL in showURL (#28) (4ce9347)

0.2.11 (2022-04-25)

Bug Fixes

0.2.10 (2022-04-16)

Bug Fixes

  • bundle xdg-open as an async chunk (890a4a9)

0.2.9 (2022-04-15)

Bug Fixes

0.2.8 (2022-04-07)

0.2.7 (2022-04-07)

Bug Fixes

  • update ssl keySize to 2048 (f63967b)

0.2.6 (2022-01-13)

Features

0.2.5 (2021-10-14)

Bug Fixes

0.2.4 (2021-04-13)

Features

  • allow recalling showURL (cd00e79)

0.2.3 (2021-04-05)

Bug Fixes

  • add \n to surrounding of listen message (b4b1300)

0.2.2 (2021-04-05)

Bug Fixes

  • add empty line to listening message (8381c49)

0.2.1 (2021-04-05)

Features

0.2.0 (2021-04-05)

⚠ BREAKING CHANGES

  • improve perf and dx

Features

0.1.4 (2021-02-22)

Features

0.1.3 (2021-02-18)

0.1.2 (2020-12-07)

Bug Fixes

0.1.1 (2020-12-07)

Features

  • types: expose Listener type (6ccb5e2)

0.1.0 (2020-12-05)

⚠ BREAKING CHANGES

  • unsupport jest as is unstable

Features

  • unsupport jest as is unstable (5ec0d07)

0.0.4 (2020-12-05)

Features

0.0.3 (2020-12-05)

Features

  • auto detect prod and test envs (d43b130)
  • clipboard and open (b4ae5b6)
  • guard against multiple close calls (f9c0315)
  • support custom certificate (d462ba5)
  • support graceful shutdown (69d6d17)

Bug Fixes

  • silently ignore clipboard and open errors (b78a6c4)

0.0.2 (2020-12-04)

0.0.1 (2020-12-04)