loopback/server/middleware/rest.js

68 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-05-03 22:50:21 +00:00
// Copyright IBM Corp. 2014,2015. All Rights Reserved.
// Node module: loopback
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
2014-06-05 00:42:18 +00:00
/*!
2013-05-24 14:59:59 +00:00
* Module dependencies.
*/
var loopback = require('../../lib/loopback');
var async = require('async');
var g = require('strong-globalize')();
2013-05-24 14:59:59 +00:00
2014-06-05 00:42:18 +00:00
/*!
2013-05-24 14:59:59 +00:00
* Export the middleware.
*/
module.exports = rest;
/**
2014-06-05 00:42:18 +00:00
* Expose models over REST.
*
2014-06-05 00:42:18 +00:00
* For example:
* ```js
* app.use(loopback.rest());
* ```
* For more information, see [Exposing models over a REST API](http://docs.strongloop.com/display/DOC/Exposing+models+over+a+REST+API).
* @header loopback.rest()
2013-05-24 14:59:59 +00:00
*/
function rest() {
var handlers; // Cached handlers
return function restApiHandler(req, res, next) {
2013-07-17 21:29:43 +00:00
var app = req.app;
var registry = app.registry;
if (!handlers) {
handlers = [];
var remotingOptions = app.get('remoting') || {};
var contextOptions = remotingOptions.context;
if (contextOptions !== undefined && contextOptions !== false) {
throw new Error(g.f(
'%s was removed in version 3.0. See %s for more details.',
'remoting.context option',
'https://docs.strongloop.com/display/APIC/Using%20current%20context'));
}
if (app.isAuthEnabled) {
var AccessToken = registry.getModelByType('AccessToken');
handlers.push(loopback.token({ model: AccessToken, app: app }));
}
handlers.push(function(req, res, next) {
// Need to get an instance of the REST handler per request
return app.handler('rest')(req, res, next);
});
}
if (handlers.length === 1) {
return handlers[0](req, res, next);
}
async.eachSeries(handlers, function(handler, done) {
handler(req, res, done);
}, next);
};
2013-05-24 14:59:59 +00:00
}