Giter VIP home page Giter VIP logo

listhen's Introduction

๐Ÿ‘‚ listhen

npm version npm downloads Codecov License JSDocs

Elegant HTTP listener!

๐Ÿ‘‰ Online Playground

Features

โœ… Dev server with HMR, static, 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

โœ… 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("/", () => "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.

License

MIT. Made with ๐Ÿ’–

listhen's People

Contributors

pi0 avatar renovate[bot] avatar danielroe avatar mastercuber avatar antfu avatar atinux avatar 0xflotus avatar aareksio avatar changjoo-park avatar farnabaz avatar louis-tian avatar mrkswrnr avatar stafyniaksacha avatar techgenius7777 avatar timursaurus avatar zenthae avatar kazupon avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.