npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

liefern

v1.0.0-rc-12

Published

Node Webserver without dependencies

Downloads

5

Readme

liefern.io

Simple, fast and no-dependency web server for nodejs.

Table of Contents

  1. Installation
  2. Quick Start
  3. Examples
  4. Features
  5. Requirements
  6. Documentation

Installation

$ npm install liefern

Quick Start

$ mkdir myapp
$ cd myapp
$ npm init
$ npm install liefern

Examples

Hello World

import { Liefern } from 'liefern'

const server = Liefern()

server.use(({ request, sharedData }) => {
  sharedData.requestIp = request.socket.remoteAddress
})

server.get('/', ({ send: { text }, sharedData }) => {
  text(`Hello World! (${sharedData.requestIp})`)
})

server.post('/', async ({ send: { text }, body }) => {
  console.log(await body())
  text('Hello World!')
})

server.start(1337)

Static files

import { Liefern } from 'liefern'
const server = Liefern()

server.static('/', `/absolutPath/to/publicHtml`)

server.start(1337)

Testing with supertest

import { Liefern } from 'liefern'

const server = Liefern({ name: 'TestServer' })
server.get('/', ({ send: { ok }}) => { 
  ok() 
})

await supertest(server.httpServer)
  .get('/')
  .expect(200)

Features

  • [x] routing
    • [x] regex url-pattern
    • [x] url-pattern-matching customizable
    • [x] all-helper for html-verb independed routes
  • [x] logging
    • [x] customizable
  • [x] full async
  • [x] exitable by signal
  • [x] appendable with middlewares
  • [x] shared data (middleware -> controller)
  • [x] serving static files
    • [x] even with middleware handling
  • [x] typescript
  • [x] ESM & CJS
  • [x] CORS
  • [x] helper for incomming request-body (json, text)
  • [x] plain node testrunner

Requirements

  • node
    • we using "node:" protocol imports (https://nodejs.org/api/esm.html#node-imports) so we need a node-version supporting this
      • 14.13.1 (if you using "import")
      • 16.0.0 (if you use "require")

Documentation

Liefern

Constructor

new Liefern({
  name?: string
  logger?: Logger
  urlMatcher?: UrlMatcherType
})

.start

const server = new Liefern()
async server.start(8080)

.stop

const server = new Liefern()
async server.start(8080)
async server.stop()

.use

const server = new Liefern()
server.use(({ request, sharedData }) => {
  sharedData.requestIp = request.socket.remoteAddress
})

.cors

server.cors({
  origin?: string[] | '*',
  allowMethods?: string[] | '*',
  exposeHeaders?: string[],
  allowHeaders?: string[] | '*',
  maxAge?: number,
  credentials?: boolean,
  secureContext?: boolean,
  privateNetworkAccess?: boolean
})

.get

server.get('/api/user/(\\d+)', ({ send: { json }, urlParams }) => {
  const [userId] = urlParams
  json({ testData: true, id: userId }) 
})

.head

server.head('/', ({ send: { ok }}) => { 
  ok() 
})

.post

server.post('/', ({ send: { ok }}) => { 
  ok() 
})

.put

server.put('/', ({ send: { ok }}) => { 
  ok() 
})

.delete

server.delete('/', ({ send: { ok }}) => { 
  ok() 
})

.patch

server.patch('/', ({ send: { ok }}) => { 
  ok() 
})

.connect

server.connect('/', ({ send: { forbidden }}) => { 
  forbidden() 
})

.trace

server.trace('/', ({ send: { notFound }}) => { 
  notFound() 
})

.options

server.options('/', ({ send: { ok }}) => { 
  ok() 
})

.all

server.all('/', ({ send: { ok }}) => { 
  ok() 
})

Controller

type Controller = (params: ControllerParams) => void | Promise<void>

ControllerParams

type ControllerParams = {
  sharedData: SharedObject
  request: Request
  response: Response
  urlParams: UrlParams
  statusCode: (statusCode: number) => void
  body: undefined | string | JsonType
  contentType: {
    textHtml: () => void
    textPlain: () => void
    applicationJson: () => void
  }
  setHeader: (name: string, value: string | number | readonly string[]) => void
  statusCodes: {
    ok: () => void
    created: () => void
    noContent: () => void
    movedPermanently: () => void
    notModified: () => void
    badRequest: () => void
    unauthorized: () => void
    paymentRequired: () => void
    forbidden: () => void
    notFound: () => void
    internalServerError: () => void
    notImplemented: () => void
  }
  send: {
    json: (data: JsonType) => void
    text: (data: string) => void
    html: (data: string) => void
    ok: () => void
    created: () => void
    noContent: () => void
    movedPermanently: (url: string) => void
    notModified: () => void
    badRequest: () => void
    unauthorized: () => void
    paymentRequired: () => void
    forbidden: () => void
    notFound: () => void
    methodNotAllow: () => void
    internalServerError: () => void
    notImplemented: () => void
  }
}

UrlMatcherType

type UrlMatcherType = (
  url: string,
  pattern: string | RegExp,
) => false | UrlParams | Promise<false | UrlParams>

UrlParams

type UrlParams = string[]

SharedObject

type SharedObject = Record<string, unknown> & {
  body: undefined | string | JsonType
}

Request

type Request = http.IncomingMessage

Response

type Response = http.ServerResponse<http.IncomingMessage> & {
  req: http.IncomingMessage
}