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

bp-router-core

v1.2.6

Published

A url router with parameter support

Downloads

58

Readme

Router-Core

A simple Javascript router for client and server side url based routing.

Setting up Router.js

Client-Side

To use Router.js in the browser, you'll need the latest v1 release of q included in a script tag on your page. After that, you need to do the same for Router.js. For example:

<script src="path/to/libs/q.js"></script>
<script src="path/to/libs/Router.js"></script>

Router.js will be available through the global Router object.

Server-Side

Router-Core can be installed via NPM by running npm install --save bp-router-core. After that, you can simply require the module. For example:

var Router = require("bp-router-core");

Using Router.js

Creating a Router

To start off, you'll need a router object. This is the core interface between third party code and the routing logic. Simply invoking new Router() will create a router object. There are no parameters, as there are currently no advanced features supported by Router.js.

Adding routes

To get the router to execute code, you need to attach functions to routes. A route consists of three things:

  • Method - This is the HTTP verb that the router will use to determine what routes to execute. This value can be any string and is case sensitive.
  • Path - The url that defines this route. This value can either be a regular expression that will be used to match against the current url, or it can be a string. In the case of the string, it must match the current url exactly with one exception. Any segment of the url that is prefixed with a colon will be used as a parameter wild card, with the appropriate value in the current url being added to the req.params object. For example, the path "/user/profile/:id" will match the url /user/profile/123456, and the req.params object will contain the key/value pair id: "123456".
  • Function - This is the function that will be executed if this route matches the current url. It will be given three parameters, although only two will commonly be needed. The first two are the request and response objects which are used to pass information along the chain of routes, while the third is the next function, used to properly exit from the route. By calling return next();, control will be passed to the next route in the stack. This is only needed if there is an instance in which the function might need to end prematurely, as the router takes care of this if the function exits naturally (reaches the end without returning anything). The alternative to returning next is to return a q promise object, which should in turn resolve with the request and response objects.

The Method can be ommited in order to apply the function to every matching url.

An example of adding a route would be

var rot = new Router();
rot.use("GET", "/user/profile/:id", function(req, res) {
  console.log("Looking for user " + req.params.id);
});

Dispatching Routes

The second piece of the puzzle is actually dispatching the routes to the router. There are four things that need to be given to the Router.route function;

  • Method - The HTTP verb that the router will attempt to handle. This basically acts as a category filter for routes.
  • Path - The url that is currently being handled
  • Request - An object that should hold all of the incoming data for this dispatch. This and Request are named to be analagous to the Node incoming/outgoing streams for a HttpServer, but do not neccesarily have to be
  • Response - An object that should be use to hold/send data responding to the dispatch

Continuing the earlier example, a GET request for the profile of user 123456 would be dispatched like so:

rot.route("GET", "/user/profile/123456", ServerReqObj, ServerResObj);

Where the last two parameters are acquired from whichever server framework is being used.

Full Example

var Router = require("bp-router-core"),
    router = new Router();

// Catch every request to log them
router.use(/.*/, function(req, res){
  console.log(req.method + " request for page " + req.path);
}

router.use("GET", "/users", function(req, res){
  displayListOfUsers();
}

router.use("POST", "/users", function(req, res){
  createNewUser();
}

router.route(getMethod(), getPath(), ServerReqObj, ServerResObj);