loopback/node_modules/resource/lib/resource.js

56 lines
1.1 KiB
JavaScript

/**
* Expose `Resource`.
*/
module.exports = Resource;
/**
* Module dependencies.
*/
var asteroid = require('asteroid')
, AsteroidModule = require('asteroid-module')
, debug = require('debug')('asteroid:resource')
, util = require('util')
, inherits = util.inherits
, assert = require('assert');
/**
* Create a new `Resource` with the given `options`.
*
* @param {Object} options
* @return {Resource}
*/
function Resource(options) {
AsteroidModule.apply(this, arguments);
// throw an error if args are not supplied
assert(typeof options === 'object', 'Resource requires an options object');
assert(options.path, 'Resource requires a path');
// 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`.
*/
inherits(Resource, AsteroidModule);
/**
* Mount the sub app on the given parent app at the configured path.
*/
Resource.prototype.mount = function (parent) {
parent.use(this.options.path, this.app);
}