Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add _setup method #91

Merged
merged 1 commit into from
Aug 11, 2014
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
4 changes: 4 additions & 0 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ module.exports = {
fn.call(self, protoService);
});

if(typeof protoService._setup === 'function') {
protoService._setup(this, location);
}

// Run the provider functions to register the service
_.each(this.providers, function (provider) {
provider(location, protoService, options);
Expand Down
42 changes: 42 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,48 @@ feathers()

You can see the combination when going to `http://localhost:8000/my/test`.

`setup` will be called after all REST routes have been set up. This means that you
can't register custom middleware with potentially conflicting paths.
In this case, implement `_setup(app, path)` instead which will run before the REST
middleware is registered. An example would be implementing a `todos/count` route
which returns the number of todos (instead of the todo with the id count):

```js
var todoService = {
todos: [
{
id: 0,
description: 'Learn Feathers'
},
{
id: 1,
description: 'Do dishes'
}
],

find: function (params, callback) {
callback(null, this.todos);
},

_setup: function (app, path) {
var self = this;

app.get('/' + path + '/count', function (req, res) {
self.find({}, function (error, todos) {
res.json({
count: todos.length
});
});
});
}
};

feathers()
.use('todos', todoService)
.listen(8000);
```


__Pro tip:__

Bind the apps `lookup` method to your service to always look your services up dynamically:
Expand Down
63 changes: 46 additions & 17 deletions test/application.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var q = require('q');
var feathers = require('../lib/feathers');

describe('Feathers application', function () {
it("Express application should use express apps", function() {
it("Express application should use express apps", function () {
var app = feathers();
var child = feathers();

Expand Down Expand Up @@ -70,15 +70,10 @@ describe('Feathers application', function () {
}
};

var oldlog = console.log;
console.log = function () {};

var app = feathers()
.configure(feathers.rest())
.configure(feathers.socketio()).use('/todo', todoService);
var server = app.listen(6999).on('listening', function () {
console.log = oldlog;

var socket = io.connect('http://localhost:6999');

request('http://localhost:6999/todo/dishes', function (error, response, body) {
Expand Down Expand Up @@ -109,7 +104,7 @@ describe('Feathers application', function () {

var app = feathers()
.configure(feathers.rest())
.use('/todo', function(req, res, next) {
.use('/todo', function (req, res, next) {
req.feathers.stuff = 'custom middleware';
next();
}, todoService)
Expand All @@ -131,7 +126,7 @@ describe('Feathers application', function () {
});
});

it('REST and SocketIO with SSL server (#25)', function(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
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
Expand All @@ -145,8 +140,6 @@ describe('Feathers application', function () {
}
};

var oldlog = console.log;
console.log = function () {};
var app = feathers()
.configure(feathers.rest())
.configure(feathers.socketio()).use('/secureTodos', todoService);
Expand All @@ -160,15 +153,13 @@ describe('Feathers application', function () {

app.setup(httpsServer);

httpsServer.on('listening', function() {
httpsServer.on('listening', function () {
var socket = io.connect('https://localhost:7889', { secure: true, port: 7889 });

console.log = oldlog;

request({
url: 'https://localhost:7889/secureTodos/dishes',
strictSSL: false,
rejectUnhauthorized : false
rejectUnhauthorized: false
}, function (error, response, body) {
assert.ok(response.statusCode === 200, 'Got OK status code');
var data = JSON.parse(body);
Expand Down Expand Up @@ -240,7 +231,7 @@ describe('Feathers application', function () {
});
});

it('throws a warning when trying to register a service after application setup (#67)', function(done) {
it('throws a warning when trying to register a service after application setup (#67)', function (done) {
var todoService = {
get: function (name, params, callback) {
callback(null, {
Expand All @@ -255,13 +246,51 @@ describe('Feathers application', function () {
.use('/todo', todoService);

var server = app.listen();
server.on('listening', function() {
server.on('listening', function () {
try {
app.use('/dummy', todoService);
done(new Error('Should throw an error'));
} catch(e) {
} catch (e) {
server.close(done);
}
});
});

it('calls _setup in order to set up custom routes with higher priority (#86)', function (done) {
var todoService = {
get: function (name, params, callback) {
callback(null, {
id: name,
description: "You have to do " + name + "!"
});
},

_setup: function (app, path) {
app.get('/' + path + '/count', function(req, res) {
res.json({
counter: 10
});
});
}
};

var app = feathers()
.configure(feathers.rest())
.use('/todo', todoService);

var server = app.listen(8999).on('listening', function () {
request('http://localhost:8999/todo/dishes', function (error, response, body) {
assert.ok(response.statusCode === 200, 'Got OK status code');
var data = JSON.parse(body);
assert.equal(data.description, 'You have to do dishes!');

request('http://localhost:8999/todo/count', function (error, response, body) {
assert.ok(response.statusCode === 200, 'Got OK status code');
var data = JSON.parse(body);
assert.equal(data.counter, 10);
server.close(done);
});
});
});
});
});
7 changes: 0 additions & 7 deletions test/providers/socketio.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ describe('SocketIO provider', function () {
};

before(function () {
// This seems to be the only way to not get the
// socket.io started log messing up the test output
var oldlog = console.log;
console.log = function () {};

app = feathers()
.configure(feathers.socketio(function(io) {
io.use(function (socket, next) {
Expand All @@ -33,8 +28,6 @@ describe('SocketIO provider', function () {

server = app.listen(7886);

console.log = oldlog;

socket = io.connect('http://localhost:7886');
});

Expand Down