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

zap

v2.1.1

Published

Lightweight HTTP server framework for Node

Downloads

2,287

Readme

⚡ zap npm CI

Zap is a lightweight HTTP server framework for Node.

Installation

Install with your favorite package manager:

$ pnpm add zap
$ yarn add zap
$ npm install zap

Usage

import {route, router, serve} from 'zap'

const app = router(
  route('GET', '/', () => 'Hello World'),

  route('GET', '/hello/:name', (req) => `Hello ${req.params.name}`),
)

const server = http.createServer(serve(app))
server.listen(3000)

API

serve(handler, options)

Constructs a new http.RequestListener out of a Handler.

router(...routes)

Constructs a new Handler out of a list of RouteHandlers.

route(method, path, handler)

Constructs a RouteHandler that matches a given method (GET, POST, etc) and path.

Body parsers

  • buffer(req, options) - read the request body as a Buffer
  • text(req, options) - read the request body as a string
  • json(req, options) - read the request body as parsed JSON

Request helpers

  • getHeader(req, header) - returns the requested header if it was provided
  • fromRequest(fn) - wraps a function in the form (req: ServerRequest, ...rest) => any to return an equivalent function that caches its results for the provided request

Response helpers

  • Ordinarily you would return a ResponseBodyType from a Handler function
  • send(res, statusCode, body) - a response with a given status code
  • notFound() - a 404 response
  • redirect(location, statusCode) - a redirect to another location (default status code 303)
  • httpError(code, message, metadata) - an error response with a given code, message, and optional metadata

Recipes

Validating body schema

You can use a function that throws an httpError to provide type-safe body payload parsing:

async function parseBody(req: ServerRequest) {
  const body = await json(req)
  if (!validate(body)) throw httpError(400, 'invalid body')
  return body
}

route('POST', '/example', (req) => {
  const body = await parseBody(req)
  // body is now typed according to your parseBody return type
})

Error handling

The serve() function options accept an errorHandler that will replace zap's built-in error handler. This allows you to report errors to services like Sentry, format the response sent to the user, etc.

serve(handler, {
  errorHandler: (_, res, error) => {
    send(res, 500, {message: 'Internal server error', details: formatError(error)})
  },
})

Credits

Special thanks to @nornagon for the zap package name. For versions of this module published before v1.0.0, see nornagon/node-zap.

License

MIT License, see LICENSE.