Named Parameters

Grapnel supports regex style routes similar to Sinatra, Restify, and Express. The properties are mapped to the parameters in the request.

router.get('products/:id?', function(req){
// GET /file.html#products/134req.params.id// => 134
});
router.get('products/*', function(req){
// The wildcard/asterisk will match anything after that point in the URL// Parameters are provided req.params using req.params[n], where n is the nth capture
});

Usage & Tips

Basic Configuration

When declaring routes with a literal object, router options can be passed as the first parameter:

var opts = { pushState :true };
Grapnel.listen(opts, routes);

Enabling PushState

var router =newGrapnel({ pushState :true });

You can also specify a root URL by setting it as an option:

var router =newGrapnel({ root :'/public/search/', pushState :true });

The root may require a beginning slash and a trailing slash depending on how your application utilizes the router.

Middleware

Grapnel uses middleware similar to how Express uses middleware. Middleware has access to the req object, event object, and the next middleware in the call stack (commonly denoted as next). Middleware must call next() to pass control to the next middleware, otherwise the router will stop.

navigate Navigate through application

on Adds a new event listener

/** * @param{String}event name (multiple events can be called when separated by a space " ") * @param{Function}callback*/router.on('myevent', function(event){
console.log('Grapnel works!');
});

once A version of on except its handler will only be called once

/** * @param{String}event name (multiple events can be called when separated by a space " ") * @param{Function}callback*/router.once('init', function(){
console.log('This will only be executed once');
});