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

http-hash-router

v2.0.1

Published

Server route handler for http-hash

Downloads

1,124

Readme

http-hash-router

Server route handler for http-hash

Example

var http = require('http');
var HttpHashRouter = require('http-hash-router');

var router = HttpHashRouter();

router.set('/health', function health(req, res) {
    res.end('OK');
});

var server = http.createServer(function handler(req, res) {
    router(req, res, {}, onError);

    function onError(err) {
        if (err) {
            // use your own custom error serialization.
            res.statusCode = err.statusCode || 500;
            res.end(err.message);
        }
    }
});
server.listen(3000);

Documentation

var router = HttpHashRouter()

type NotFoundError : Error & {
    type: "http-hash-router.not-found",
    statusCode: 404
}

type Router : {
    set: (pattern: String, handler: Function | Object) => void
} & (
    req: HttpReqest,
    res: HttpResponse,
    opts: Object,
    cb: Callback<NotFoundError | Error, void>
) => void

http-hash-router : () => Router

HttpHashRouter will create a new router function.

The HttpHashRouter itself takes no options and returns a function that takes four arguments, req, res, opts, cb.

router(req, res, opts, cb)

type NotFoundError : Error & {
    type: "http-hash-router.not-found",
    statusCode: 404
}

router : (
    req: HttpReqest,
    res: HttpResponse,
    opts: Object,
    cb: Callback<NotFoundError | Error, void>
) => void
  • throw http-hash-router.expected.callback exception.

It is expected that you call the router function with the HTTPRequest and HTTPResponse as the first and second arguments.

The third argument is the options object. The router will copy the options object and set the params and splat field.

The fourth argument is a callback function, this function either gets called with a http-hash-router.not-found error or gets passed to the route handler function.

If you do not pass a callback to the router function then it will throw the http-hash-router.expected-callback exception.

router.set(pattern, handler)

type RoutePattern : String
type RouteHandler : Object<method: String, RouteHandler> | (
    req: HttpRequest,
    res: HttpResponse,
    opts: Object & {
        params: Object<String, String>,
        splat: String | null
    },
    cb: Callback<Error, void>
) => void

set : (RoutePattern, RouteHandler) => void

You can call .set() on the router and it will internally store your handler against the pattern.

.set() takes a route pattern and a route handler. A route handler is either a function or an object. If you use an object then we will create a route handler function using the http-methods module.

The .set() functionality is implemented by http-hash itself and you can find documentation for it at HttpHash#set.

Your handler function will get called with four arguments.

  • req the http request stream
  • res the http response stream
  • opts options object. This contains properties defined in the server and also contains the params and splat fields.
  • cb callback.

If your route pattern contains a param, i.e. "/foo/:bar" or your route pattern contains a splat, i.e. "/foo/*" then the values of the params and splat will be passed to the params and splat field on opts.