listhen: HTTP Listener and Dev Server

1.9.1 · active · verified Tue Apr 21

listhen is an elegant HTTP listener for Node.js, designed to streamline local development server setups. It supports various server handlers including Node.js built-in HTTP, Express, and `unjs/h3`, making it highly adaptable for different project types. Currently at version 1.9.1, the package maintains an active release cadence, frequently publishing minor improvements and bug fixes, as evidenced by recent releases such as v1.9.1, v1.9.0, and v1.8.0. Key differentiators include its rich feature set out-of-the-box, such as hot module reloading (HMR) capabilities, static file serving, robust TypeScript support via `unjs/jiti`, and integrated WebSocket support through `unjs/crossws`. It also enhances developer experience with features like automatic port finding (via `get-port-please`), displaying QR codes for public URLs (with `unjs/uqr`), local server tunneling (using `unjs/untun`), clipboard integration for URLs, and simplified HTTPS setup with self-signed certificates. listhen aims to provide a comprehensive, zero-configuration development server experience, abstracting away common setup complexities.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to create an h3 application and listen to it using `listhen`, showcasing essential options like port, hostname, URL clipboard copying, and automatic browser opening.

import { createApp, eventHandler } from "h3";
import { listen } from "listhen";

const app = createApp();

app.use(
  "/",
  eventHandler(() => "Hello world from listhen and h3!")
);

async function startServer() {
  const listener = await listen(app, {
    port: process.env.PORT ? parseInt(process.env.PORT) : 3000, // Use process.env.PORT or fallback to 3000
    hostname: process.env.HOST || "0.0.0.0",
    clipboard: true, // Copy URL to clipboard in dev
    open: true,      // Open in browser in dev
    showURL: true,   // Show URL in console
    https: false     // Set to true for self-signed HTTPS in dev
  });
  console.log(`Server listening on ${listener.url}`);

  // Graceful shutdown on common signals
  process.on('SIGINT', () => { listener.close(); console.log('Server gracefully shutting down...'); });
  process.on('SIGTERM', () => { listener.close(); console.log('Server gracefully shutting down...'); });
}

startServer();

view raw JSON →