Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions 16 lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = {
});
},

service: function(location, service) {
service: function(location, service, options) {
var protoService = Proto.extend(service);
var self = this;

Expand All @@ -31,15 +31,17 @@ module.exports = {

// Run the provider functions to register the service
_.each(this.providers, function (provider) {
provider(location, protoService);
provider(location, protoService, options);
});

this.services[location] = protoService;
return this;
},

use: function (location, service) {

use: function () {
var args = _.toArray(arguments);
var location = args.shift();
var service = args.pop();
var hasMethod = function() {
return _.some(arguments, function(name) {
return (service && typeof service[name] === 'function');
Expand All @@ -51,8 +53,10 @@ module.exports = {
return this._super.apply(this, arguments);
}

return this.service(location, service);

return this.service(location, service, {
// Any arguments left over are other middleware that we want to pass to the providers
middleware: args
});
},

lookup: function (location) {
Expand Down
37 changes: 19 additions & 18 deletions 37 lib/providers/rest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = function (config) {
});
};

if(typeof config === 'function') {
if (typeof config === 'function') {
handler = config;
}

Expand All @@ -31,28 +31,29 @@ module.exports = function (config) {
app.rest = wrappers;

// Register the REST provider
app.providers.push(function (path, service) {
app.providers.push(function (path, service, options) {
if (app.disabled('feathers rest')) {
return;
}

var middleware = (options && options.middleware) || [];
var uri = path.indexOf('/') === 0 ? path : '/' + path;

app.route(uri)
// GET / -> service.find(cb, params)
.get(app.rest.find(service))
// POST -> service.create(data, params, cb)
.post(app.rest.create(service));

app.route(uri + '/:id')
// GET /:id -> service.get(id, params, cb)
.get(app.rest.get(service))
// PUT /:id -> service.update(id, data, params, cb)
.put(app.rest.update(service))
// PATCH /:id -> service.patch(id, data, params, callback)
.patch(app.rest.patch(service))
// DELETE /:id -> service.remove(id, params, cb)
.delete(app.rest.remove(service));
var baseRoute = app.route(uri);
var idRoute = app.route(uri + '/:id');

// GET / -> service.find(cb, params)
baseRoute.get.apply(baseRoute, middleware.concat(app.rest.find(service)));
// POST -> service.create(data, params, cb)
baseRoute.post.apply(baseRoute, middleware.concat(app.rest.create(service)));

// GET /:id -> service.get(id, params, cb)
idRoute.get.apply(idRoute, middleware.concat(app.rest.get(service)));
// PUT /:id -> service.update(id, data, params, cb)
idRoute.put.apply(idRoute, middleware.concat(app.rest.update(service)));
// PATCH /:id -> service.patch(id, data, params, callback)
idRoute.patch.apply(idRoute, middleware.concat(app.rest.patch(service)));
// DELETE /:id -> service.remove(id, params, cb)
idRoute.delete.apply(idRoute, middleware.concat(app.rest.remove(service)));

app.use(uri, handler);
});
Expand Down
35 changes: 35 additions & 0 deletions 35 test/application.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,41 @@ describe('Feathers application', function () {
});
});

it('uses custom middleware (#21)', function (done) {
var todoService = {
get: function (name, params, callback) {
callback(null, {
id: name,
description: "You have to do " + name + "!",
stuff: params.stuff
});
}
};

var app = feathers()
.configure(feathers.rest())
.use('/todo', function(req, res, next) {
req.feathers.stuff = 'custom middleware';
next();
}, todoService)
.use('/otherTodo', todoService);

var server = app.listen(6995).on('listening', function () {
request('http://localhost:6995/todo/dishes', function (error, response, body) {
assert.ok(response.statusCode === 200, 'Got OK status code');
var data = JSON.parse(body);
assert.equal(data.stuff, 'custom middleware', 'Custom middleware updated params');

request('http://localhost:6995/otherTodo/dishes', function (error, response, body) {
assert.ok(response.statusCode === 200, 'Got OK status code');
var data = JSON.parse(body);
assert.ok(!data.stuff, 'Custom middleware not run for different service.');
server.close(done);
});
});
});
});

it('REST and SocketIO with SSL server (#25)', function(done) {
// For more info on Reqest HTTPS settings see https://github.com/mikeal/request/issues/418
// This needs to be set so that the SocektIO client can connect
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.