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

httpanda

v0.4.7

Published

A frugal HTTP server for simple tasks.

Downloads

2,524

Readme

🐼 HTTPanda - a frugal HTTP server for simple tasks

  • Lightweight and fast
  • Streamlined feature set, sufficient for most applications
  • Supports express (and connect) compatible middleware
  • Both CommonJS and ES module support via tsukuru

Basic usage

import { Server } from 'httpanda';
import cors from 'cors';

const server = new Server();
server.use(cors())
      .get('/hello', (req, res) => res.end('Hello World!'))
      .get('/users/:id', (req, res) => res.end(`Welcome, user ${req.params.id}!`))
      .listen(3000).then(() => console.log('Listening on port 3000')).catch(e => console.error(e));

Reference

new Server(options?: ServerOptions)

Creates a new instance of HTTPanda.

All options are optional, you can just use new Server() to use the default options.

Available options:

server

Your own HTTP server instance. Can also take a https.Server to support SSL.

onError

An error handler that takes the following arguments:

  • e - the error object
  • req - the request object (an instance of the internal Request type)
  • res - the response object (an instance of the internal Response type, currently equivalent to http.ServerResponse)
  • next - a function you can call to ignore the error and keep walking the middleware chain

Example

const server = new Server({
    server: https.createServer({ /* ... certificate stuff ... */ }),
    onError: (e, res) => {
        console.log(e);
        res.statusCode = e.code || 500;
        res.end(JSON.stringify(e));
    }
})

server.listen(port: number): Promise<this>

Listens to the given port. The returned Promise resolves with the Server instance on success, or rejects in case the port is in use or could otherwise not be bound.

Other forms of this as shown in the node net documentation are also supported, except the callback is always replaced by the returned Promise.

server.use(...callbacks: RouteCallback[]): this

server.use(path: string, ...callbacks: RouteCallback[]): this

Chains a set of middlewares on the given path, or on any path if none is given. Express middlewares are supported!

server.add(method: string | undefined, path: string, ...callbacks: RouteCallback[]): this

Adds a callback to a specific path. Parameters can be specified on the path with a leading colon and are available on req.params:

server.get('/users/:id', (req, res) => res.end(`Welcome, user ${req.params.id}!`))

If you pass undefined as method, the callback will execute on any method.

server.all(path: string, ...callbacks: RouteCallback[]): this

A shortcut to server.add(undefined, path, ...callbacks).

server.get(path: string, ...callbacks: RouteCallback[]): this

A shortcut to server.add('GET', path, ...callbacks).

Other shortcuts that work the same way:

  • server.head
  • server.options
  • server.post
  • server.put
  • server.patch
  • server.delete
  • server.connect
  • server.trace