0.0.3 โ€ข Published 12 months ago

earlist v0.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

cover npm version npm downloads bundle JSDocs License

๐Ÿ‘‚ earlist

๐Ÿ‘‚ An elegant HTTP listener.

โœจ Features

  • Promisified interface for listening and closing server โœจ
  • Work with express/connect or plain http handle function ๐Ÿ”„
  • Support HTTP and HTTPS ๐Ÿ”’
  • Assign a port or fallback to human friendly alternative (with scotty-beam-me-up) ๐ŸŒ
  • Generate listening URL and show on console ๐Ÿ“ก
  • Copy URL to clipboard (dev only by default) ๐Ÿ“‹
  • Open URL in browser (opt-in) ๐ŸŒ๐Ÿ”
  • Generate self-signed certificate ๐Ÿ“œ
  • Detect test and production environments ๐Ÿงช๐Ÿš€
  • Close on exit signal โ›”
  • Gracefully shutdown server with http-shutdown ๐Ÿ› ๏ธ

โšก๏ธ Install

Install:

#nyxi
nyxi earlist

#pnpm
pnpm add earlist

#npm
npm i earlist

#yarn
yarn add earlist

Import into your Node.js project:

// CommonJS
const { listen } = require('earlist')

// ESM
import { listen } from 'earlist'

๐ŸŽฏ Usage

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)

โš™๏ธ Options

โš“๏ธ port

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, earlist 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 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 --.

๐ŸŒ showURL

  • Default: true (force disabled on test environment)

Show a CLI message for listening URL.

๐ŸŒ baseURL

  • Default: /

๐Ÿ” open

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

Open URL in browser. Silently ignores errors.

๐Ÿ“‹ clipboard

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

Copy URL to 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 signal is received on process.

๐Ÿ“œ License

MIT - Made with ๐Ÿ’ž