0.3.2 • Published 10 years ago

crowbar v0.3.2

Weekly downloads
5
License
BSD
Repository
github
Last release
10 years ago

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": { }
});
0.3.2

10 years ago

0.3.1

10 years ago

0.3.0

10 years ago

0.2.3

10 years ago

0.2.2

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.33

10 years ago

0.1.32

10 years ago

0.1.31

10 years ago

0.1.30

10 years ago

0.1.29

10 years ago

0.1.28

10 years ago

0.1.27

10 years ago

0.1.26

10 years ago

0.1.25

10 years ago

0.1.24

10 years ago

0.1.23

10 years ago

0.1.22

10 years ago

0.1.21

10 years ago

0.1.19

10 years ago

0.1.18

10 years ago

0.1.16

10 years ago

0.1.15

10 years ago

0.1.14

10 years ago

0.1.13

10 years ago

0.1.12

10 years ago

0.1.11

10 years ago

0.1.10

10 years ago

0.1.9

10 years ago

0.1.8

10 years ago

0.1.7

10 years ago

0.1.6

10 years ago

0.1.5

10 years ago

0.1.3

10 years ago

0.0.0

11 years ago