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

browserver-router

v0.1.1

Published

A platform-agnostic router for HTTP listeners that follow the node.js spec

Downloads

4

Readme

browserver-router

Build Status

This is a simple and unambitious router implementation that can be used either in the browser, or in any CommonJS environment. It has no dependencies, and weighs in at 462 minizipped bytes. It was designed for browserver, but will work with any server that conforms to the node.js HTTP spec (in which handlers take have a (req, res) signature, where req has url and method properties, and res has writeHead and end methods).

Example

In node.js

Router = require("router")

var router = new Router({
  "/salutation/:name": {
    GET: function(req, res) {
      res.writeHead(200)
      res.end("Hello, " + req.params[0] + ".")
    },

    DELETE: function(req, res) {
      res.writeHead(200)
      res.end("Goodbye, " + req.params[0] + ".")
    }
  },

  "/method": function(req, res) {
    res.writeHead(200)
    res.end("Matched method: " + req.method)
  }
})

var http = require("http")
var server = http.createServer(router.onrequest)

server.listen(8000)

In the browser with browserver and engine.io

var router = new Router({
  "/salutation/:name": {
    GET: function(req, res) {
      res.writeHead(200)
      res.end("Hello, " + req.params[0] + ".")
    },

    DELETE: function(req, res) {
      res.writeHead(200)
      res.end("Goodbye, " + req.params[0] + ".")
    }
  },

  "/method": function(req, res) {
    res.writeHead(200)
    res.end("Matched method: " + req.method)
  }
})

var server = http.createServer(router.onrequest)
var ws = new eio.Socket({host: "myserver.com"})

server.listen(ws)

API

router = new Router([Object routes])

Creates a new router. routes is optional, and can be an object where each key is a route pattern, and each value is either a route handler function, or object with methods for keys and route handler functions for values.

router.route([String route], [Function handler])

router.route([String route], [Object methodMap])

Adds a route to match. Both arguments are required. The route string is compiled into a regular expression, using the same logic as the Backbone.js router, in which :param strings match a single url component between slashes and * splats match any number of url components. Any matching parameters are used to populate the req.params array by match position.

If a handler function is provided, it will be fired when the route is matched, for any request method.

If a methodMap object is provided, it will be used to disambiguate between methods of a given route by specifying them as keys, with the handlers for the values.

router[404]

router[405]

These properties hold the default handlers. Override them to provide your own fallback logic.