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

crowbar

v0.3.2

Published

HTTP Router for Mojo.js

Downloads

28

Readme

Crowbar HTTP Router Alt ci

Testling

Crowbar is a flexible routing system inspired by express, and director.

Example

var router = require("crowbar")();

function auth (location, next) {
  // authenticate here
}

router.param("classroom", function (location, next) {
  // load classroom
  next(null, classroom);
});  

router.add({
  enter: auth,
  "/classes/:classroom": {
    "/reports": {
      enter: function (location, next) {
        // do stuff with route
      }
    }
  }
});

router.redirect("/classes/classid/reports", function (err, location) {
  console.log(location.get("params.classroom")); // classroom model
  console.log(location.get("pathname")); // /classes/classid/reports
  console.log(location.get("url")); // same as pathname, but also includes query params
});

Entering Routes

Called when a route is entered.

router.add({
  "/home": {
    enter: function (location, next) {
      // do stuff
      next(); // continue
    }
  }
});

router.redirect("/home", function (err, location) {

});

You can also specify multiple enter handlers:

router.add({
  "/home": {
    enter: [auth, function (location, next) {
      // do stuff
      next(); // continue
    }]
  }
});

API

Exiting Routes

Useful if you want to teardown a route before entering another.

router.add({
  "/contact": {
    exit: function (location, next) {
      next();
    },
    enter: function (location, next) {
      next();
    }
  },
  "/home": {
    enter: function (location, next) {
      next(); // continue
    }
  }
});

router.redirect("/contact");
router.redirect("/home"); // exit handler called

Just like enter handlers, you can specify multiple exit handlers

Route States

States are properties set by the router which may modify your application state. This is used specifically in mojo.js.

router.add({
  "/classes/:classroom": {
    states: { app: "classes" },
    "/reports": {
      states: { classes: "reports" }
    }
  }
});

router.bind("location.states", function (states) {
  // { app: "classes", classes: "reports" }
});

router.redirect("/classes/classid/reports");

Parameters

Just like express.js, you have the ability to create parameter loaders.

router.param("classroom", function (location, next) {
  console.log("location.params.classroom"); // classid
  next(null, classroomModel);
});

router.add({
  "/classes/:classroom": {}
});

router.redirect("/classes/classid", function (err, location) {
  console.log(location.get("params.classroom")); // classroomModel
})

Naming Routes

router.add({
  "/classes/:classroom": {
    name: "classroom"
  }
});

router.redirect("classroom", {
  params: {
    classroom: "classid"
  }
}, function (err, location) {

});

listeners

Kubrick comes with an HTTP listener by default, which is loaded automatically in the browser.

router.redirect(pathnameOrRouteName[, options], complete)

  • pathnameOrRouteName - pathname or route name to redirect to
  • options - route name options
    • query - route query
    • params - route params
  • complete - called when redirected

router.add(routes)

adds new routes to the router

router.use(plugins...)

adds plugins to the router

router.location

The current location of the router

router.bind("location", function () {
  // called whenever the location changes
});

Routes router.routes

Routes property

routes.find(query)

Finds a route based on the query.

router.add({
  "/home": {
    name: "homeRoute"
  }
});

console.log(router.routes.find({ pathname: "/home" })); // /home route
console.log(router.routes.find({ pathname: "homeRoutek " })); // /home route

location.query

query parameters on the location. Note that if the query changes, those changes will also be reflected in the HTTP url.

router.bind("location", function (err, location) {
  console.log(location.get("query.hello")); // blah
  location.set("query.hello", "world"); // gets reflected in the HTTP url
});

router.redirect("/home?hello=blah");

location.params

similar to location.query. location.params are taken from the route parameters.

location.url

pathname + query params.

router.bind("location", function (err, location) {
  console.log(location.get("url")); // /home?hello=blah
});

router.redirect("/home?hello=blah");

location.pathname

just the pathname of the location

location.equals(location)

returns TRUE of the both locations are the same

location.redirect(pathname, options)

redirects the location

Mojo.js Usage

basic usage:

var mojo = require("mojojs"),
app = new mojo.Application();
app.use(require("crowbar"));

app.router.add({
  "/home": { }
});