Grapnel Server-Side Router Framework for Node.js
Simple JavaScript Router for Node.js with Named Parameters and Middleware support.
Download/Installation
Install with npm
npm install grapnel-server
Grapnel Features
- Supports Named Parameters similar to Sinatra, Restify, and Express
- Middleware Support
- RegExp Support
- No dependencies
Server-Side Routing
var http = app = ; app; http;
Named Parameters
Grapnel.js supports regex style routes similar to Sinatra, Restify, and Express. The properties are mapped to the parameters in the request.
app; app;
Middleware Support
Grapnel.js also supports middleware:
var { user;} app;
Route Context
You can add context to a route and even use it with middleware:
var usersRoute = router; // Middleware can be used here usersRoute; usersRoute; usersRoute; // GET /user/13589// => Profile 13589 // GET /user/13589/followers// => Followers 13589 // POST /user/13589// => POSTED to 13589
Declaring Multiple Routes
var Grapnel = Server; var routes = { // GET /products } { // GET /products/widgets/35 reqparamscategory // => widgets } Grapnel;
Event Handling
var app = ; app;
RegExp Support
Grapnel.js allows RegEx when defining a route:
var expression = /^\/food\/tacos\/$/i; app;
Usage & Tips
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.
For more information about how middleware works, see Using Middleware.
var { user;} app;
Stopping a Route Event
app;
Stopping Event Propagation
app; app; // GET /products/35
404 Pages
You can specify a route that only uses a wildcard *
as your final route, then use req.event.parent()
which returns false
if the call stack didn't run any route handlers.
// This should be your last routeappall'*' { if!thisstateparent res; res; };
API Documentation
get
, post
, put
, delete
(HTTP Method) Adds a listeners and middleware for routes matching its respective HTTP method
/** * @param * @param */app; app; app; app; appall'/store/' { // This will be called with any HTTP method};
on
Adds a new event listener
/** * @param * @param*/router;
once
A version of on
except its handler will only be called once
/** * @param * @param*/router;
trigger
Triggers an event
/** * @param * @param*/app;
context
Returns a function that can be called with a specific route in context.
Both the router.context
method and the function it returns can accept middleware. Additionally, you can specify which HTTP method (GET, POST, PUT, DELETE) should be routed by the callback. Not specifying an HTTP method will assume GET.
Note: when calling route.context
, you should omit the trailing slash.
/** * @param * @param {[Function]} Middleware (optional) * @return */var usersRoute = router; usersRoute; // Not specifying an HTTP method assumes GET; // GET /user/13589/followers// => Followers 13589 // POST /user/13589// => Followers 13589
bind
An alias of on
add
An alias of get
path
router.path('string')
Sets a new path or hashrouter.path()
Gets path or hashrouter.path(false)
Clears the path or hash
fragment
(Deprecated)
Events
navigate
Fires when http module initializes a new requestmatch
Fires when a new match is found, but before the handler is called