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-route

v1.2.0

Published

HTTP routing with a nestable functional style

Downloads

14

Readme

http-route

HTTP routing with a nestable functional style.

This module exports a function that wraps a given http handler and returns a new http handler that is only executed if the given condition is met.

Usage

var route = require("http-route");
var compose = require("http-compose");
var createServer = require("http").createServer;

createServer(compose([
  route(isWrite, checkCSRF),
  route("/hello", compose([
    route("GET /:name", sendHello),
    route("GET /", sendDefaultHello),
  ]),
]).listen(8080);

function isWrite(req) {
  return ["PUT", "POST", "DELETE"].indexOf(req.method) !== -1;
}

function checkCSRF(req, res, callback) {
  if (req.headers["x-csrf-token"] !== "magic") {
    res.statusCode = 400;
    res.end("Invalid request");
    return;
  }
  callback();
}

function sendHello(req, res) {
  res.end("hello, " + req.params.name);
}

function sendDefaultHello(req, res) {
  res.end("hello world");
}

Conditions

Conditions are functions that determine if a request matches the route or not. A condition function can contain any arbitrary logic, as long as it synchronously returns a truthy or falsey value.

There are also higher-order conditions, such as strings, that get "compiled" down to a condition function. For example, the string "GET /hello" is equivalent to the following condition function:

function(req) {
  return req.method === "GET" && req.url === "/hello";
}

Condition functions can also modify the state of the current request. For example, condition functions can modify req.url so that nested routes can match only the remaining part of the URL. They can also parse out parameters from the URL and populate something like req.params.

Functions

Any arbitrary function with a return value that will determine if the route matches or not. The function receives the current request as a parameter.

Strings

Strings can contain a method, a url, or both.

Methods

"GET" will match GET requests.

URLs

"/foo" will match requests that begin with "/foo". URLs can contain named parameters in the same way as the express router and will populate req.params. The current req.url will be modified to remove the matched part of the URL so that nested routers can match on the remaining unmatched part of the URL. The original full URL is saved in req.originalUrl.

Both

"POST /bar" will match POST requests to the exact url "/bar".

Objects

  • path: The request path must match the given path exactly
  • mount: The request path must begin with the given path and will be "mounted" under the given path (req.url will be adjusted to reflect this)
  • method: The request method must match the given method

Arrays

An array of conditions will be composed into a single condition and match only if all conditions match.

HTTP Handlers

HTTP handlers are functions that handle http requests. These are the same type of functions passed to require("http").createServer.

Composing HTTP Handlers

The http-compose module (not yet written) can be used to compose multiple http handlers into a single handler. A third callback parameter will be passed to composed functions to allow passing control to the next handler in the chain.

API

route(condition, handler)

Creates an http handler function that will only be called if the given condition matches.

Installation

npm install http-route