Skip to content

Commit

Permalink
Adding docs to route module
Browse files Browse the repository at this point in the history
  • Loading branch information
esatterwhite committed Dec 27, 2016
1 parent dd7429d commit 26f91d2
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions lib/server/router.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,99 @@
/*jshint laxcomma: true, smarttabs: true, node: true, esnext: true*/
'use strict';
/**
* Simple router class for directing requests
* @module skyring/lib/server/router
* @author Eric Satterwhite
* @since 1.0.0
* @requires skyring/lib/server/route
* @requires skyring/lib/server/request
* @requires skyring/lib/server/response
*/

const Route = require('./route')
, Request = require('./request')
, Response = require('./response')
;

/**
* @constructor
* @alias module:skyring/lib/server/router
* @param {module:skyring/lib/server/node} node The node linked to the application hashring
* @example var x = new Router(node)
*/
function Router( node, opts ) {
this.routes = new Map();
this.route_options = new Map();
this.node = node;
};

/**
 * Adds a new get handler to the router a new get handler to the router
 * @param {String} The url path to route on
 * @param {Function} The handler function to call when the route is matched
 * @return
 **/
Router.prototype.get = function get( path, fn ) {
this.route( path, 'GET', fn );
};

/**
 * Adds a new put handler to the router
 * @param {String} The url path to route on
 * @param {Function} The handler function to call when the route is matched
 * @return
 **/
Router.prototype.put = function put( path, fn ) {
this.route( path, 'PUT', fn);
};

/**
 * Adds a new post handler to the router
 * @param {String} The url path to route on
 * @param {Function} The handler function to call when the route is matched
 * @return
 **/
Router.prototype.post = function post( path, fn ) {
this.route( path, 'POST', fn);
};

/**
 * Adds a new patch handler to the router
 * @param {String} The url path to route on
 * @param {Function} The handler function to call when the route is matched
 * @return
 **/
Router.prototype.patch = function patch( path, fn ) {
this.route( path, 'PATCH', fn);
};

/**
 * Adds a new delete handler to the router
 * @param {String} The url path to route on
 * @param {Function} The handler function to call when the route is matched
 * @return
 **/
Router.prototype.delete = function( path, fn ) {
this.route( path, 'DELETE', fn );
};

/**
 * Adds a new opts handler to the router
 * @param {String} The url path to route on
 * @param {Function} The handler function to call when the route is matched
 * @return
 **/
Router.prototype.options = function options( path, fn ) {
this.route( path, 'OPTIONS', fn );
};

/**
 * Adds a new route handler to the router
 * @param {String} The url path to route on
* @param {String} method The http method to associate to the route
 * @param {Function} The handler function to call when the route is matched
 * @return
 **/
Router.prototype.route = function route( path, method, fn ) {
const _method = method.toUpperCase();
const map = this.routes.get(_method) || new Map();
Expand All @@ -52,6 +111,17 @@ Router.prototype.route = function route( path, method, fn ) {
return route;
};

/**
 * Entrypoint for an incoming request
* Customer properties are attached to an `$` object on the request rather than the request
* itself to avoid V8 deopts / perf penalties
 * @param {http.IncomingMessage} req
 * @param {http.ServerResponse} res
* @example
http.createServer((req, res) => {
router.handle(req, res)
})
 **/
Router.prototype.handle = function handle( req, res ) {
req.$ = new Request( req );
res.$ = new Response( res );
Expand All @@ -78,6 +148,13 @@ Router.prototype.handle = function handle( req, res ) {
return notFound( req, res );
};

/**
 * Responsible for executing the middleware stack on the route ( including the end handler )
 * @param {module:skyring/lib/server/route} route
 * @param {http.IncomingMessage} req
* @param {http.ServerResponse} res
 * @return
 **/
Router.prototype.handleRoute = function handleRoute( route, req, res ) {
route.process(req, res, this.node, ( err ) => {
if ( err ) return res.$.error( err );
Expand Down

0 comments on commit 26f91d2

Please sign in to comment.