2013-04-09 18:33:29 +00:00
|
|
|
/**
|
2013-04-18 00:43:00 +00:00
|
|
|
* Expose `Route`.
|
2013-04-09 18:33:29 +00:00
|
|
|
*/
|
|
|
|
|
2013-04-18 00:43:00 +00:00
|
|
|
module.exports = Route;
|
2013-04-09 18:33:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var asteroid = require('asteroid')
|
|
|
|
, AsteroidModule = require('asteroid-module')
|
2013-04-10 17:55:13 +00:00
|
|
|
, HttpContext = require('./http-context')
|
2013-04-09 18:33:29 +00:00
|
|
|
, debug = require('debug')('asteroid:resource')
|
|
|
|
, util = require('util')
|
|
|
|
, inherits = util.inherits
|
|
|
|
, assert = require('assert');
|
|
|
|
|
|
|
|
/**
|
2013-04-18 00:43:00 +00:00
|
|
|
* Create a new `Route` with the given `options`.
|
2013-04-09 18:33:29 +00:00
|
|
|
*
|
|
|
|
* @param {Object} options
|
2013-04-18 00:43:00 +00:00
|
|
|
* @return {Route}
|
2013-04-09 18:33:29 +00:00
|
|
|
*/
|
|
|
|
|
2013-04-18 00:43:00 +00:00
|
|
|
function Route(options) {
|
2013-04-09 18:33:29 +00:00
|
|
|
AsteroidModule.apply(this, arguments);
|
|
|
|
|
|
|
|
// throw an error if args are not supplied
|
2013-04-18 00:43:00 +00:00
|
|
|
assert(typeof options === 'object', 'Route requires an options object');
|
|
|
|
assert(options.path, 'Route requires a path');
|
2013-04-09 18:33:29 +00:00
|
|
|
|
|
|
|
// create the sub app
|
|
|
|
var app = this.app = asteroid();
|
|
|
|
|
|
|
|
this.options = options;
|
|
|
|
|
|
|
|
debug('created with options', options);
|
|
|
|
|
|
|
|
this.on('destroyed', function () {
|
|
|
|
app.disuse(this.options.path);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inherit from `AsteroidModule`.
|
|
|
|
*/
|
|
|
|
|
2013-04-18 00:43:00 +00:00
|
|
|
inherits(Route, AsteroidModule);
|
2013-04-09 18:33:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Mount the sub app on the given parent app at the configured path.
|
|
|
|
*/
|
|
|
|
|
2013-04-18 00:43:00 +00:00
|
|
|
Route.prototype.mount = function (parent) {
|
2013-04-10 17:55:13 +00:00
|
|
|
this.parent = parent;
|
2013-04-09 18:33:29 +00:00
|
|
|
parent.use(this.options.path, this.app);
|
2013-04-10 17:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an http context bound to the current resource.
|
|
|
|
*/
|
|
|
|
|
2013-04-18 00:43:00 +00:00
|
|
|
Route.prototype.createContext = function (req, res, next) {
|
2013-04-10 17:55:13 +00:00
|
|
|
return new HttpContext(this, req, res, next);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override `on` to determine how many arguments an event handler expects.
|
|
|
|
*/
|
|
|
|
|
2013-04-18 00:43:00 +00:00
|
|
|
Route.prototype.on = function () {
|
2013-04-10 17:55:13 +00:00
|
|
|
var fn = arguments[arguments.length - 1];
|
|
|
|
|
|
|
|
if(typeof fn === 'function') {
|
|
|
|
// parse expected arguments from function src
|
|
|
|
// fn.listener handles the wrapped function during `.once()`
|
|
|
|
var src = (fn.listener || fn).toString();
|
|
|
|
fn._expects = src.split('{')[0].split(',').length;
|
|
|
|
}
|
|
|
|
|
|
|
|
AsteroidModule.prototype.on.apply(this, arguments);
|
2013-04-09 18:33:29 +00:00
|
|
|
}
|