8000 Upgrade to Express 4 by daffl · Pull Request #55 · feathersjs/feathers · GitHub
[go: up one dir, main page]

Skip to content
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
10 changes: 10 additions & 0 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ module.exports = {
return this;
},

// Express 3.x configure is gone in 4.x but we'll keep a more basic version
// That just takes a function in order to keep Feathers plugin configuration easier.
// Environment specific configurations should be done as suggested in the 4.x migration guide:
// https://github.com/visionmedia/express/wiki/Migrating-from-3.x-to-4.x
configure: function(fn){
fn.call(this);

return this;
},

listen: function () {
var server = this._super.apply(this, arguments);
this.setup(server);
Expand Down
8 changes: 1 addition & 7 deletions lib/feathers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@ var providers = require('./providers');
* @api public
*/

function createApplication(config) {
function createApplication() {
var app = express();
Proto.mixin(Application, app);
app.init();

// Add REST provider by default, can always be disabled using app.disable('feathers rest')
if(!config || config.rest) {
app.use(express.urlencoded()).use(express.json()).configure(providers.rest());
}

return app;
}

Expand Down
24 changes: 14 additions & 10 deletions lib/providers/rest/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

var bodyParser = require('body-parser');
var wrappers = require('./wrappers');

module.exports = function (config) {
Expand All @@ -22,7 +23,7 @@ module.exports = function (config) {

app.enable('feathers rest');

app.use(function (req, res, next) {
app.use(bodyParser()).use(function (req, res, next) {
req.feathers = {};
next();
});
Expand All @@ -37,18 +38,21 @@ module.exports = function (config) {

var uri = path.indexOf('/') === 0 ? path : '/' + path;

// GET / -> service.find(cb, params)
app.get(uri, app.rest.find(service))
// GET /:id -> service.get(id, params, cb)
.get(uri + '/:id', app.rest.get(service))
app.route(uri)
// GET / -> service.find(cb, params)
.get(app.rest.find(service))
// POST -> service.create(data, params, cb)
.post(uri, app.rest.create(service))
.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(uri + '/:id', app.rest.update(service))
// DELETE /:id -> service.remove(id, params, cb)
.del(uri + '/:id', app.rest.remove(service))
.put(app.rest.update(service))
// PATCH /:id -> service.patch(id, data, params, callback)
.patch(uri + '/:id', app.rest.patch(service));
.patch(app.rest.patch(service))
// DELETE /:id -> service.remove(id, params, cb)
.delete(app.rest.remove(service));

app.use(uri, handler);
});
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@
},
"dependencies": {
"uberproto": "~1.x",
"express": "~3.4.0",
"express": "~4.0.0",
"rubberduck": "~1.x",
"socket.io": "~0.9.0",
"primus-emitter": "~3.x",
"primus": "~2.x",
"lodash": "~2.4.1"
"lodash": "~2.4.1",
"body-parser": "~1.0.2"
},
"devDependencies": {
"request": "~2.x",
Expand Down
15 changes: 4 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ If you are not familiar with Express head over to the [Express Guides](http://ex

### REST

Exposing services through a RESTful JSON interface is enabled by default. If you only want to use SocketIO call `app.disabled('feathers rest')` _before_ registering any services.
In almost every case you want to exposes your services through a RESTful JSON interface. This can be achieved by calling `app.configure(feathers.rest())`.

To set service parameters in a middleware, just attach it to the `req.feathers` object which will become the params for any resulting service call:

```js
app.use(function(req, res) {
app.configure(feathers.rest()).use(function(req, res) {
req.feathers.data = 'Hello world';
});

Expand All @@ -29,15 +29,10 @@ app.use('/todos', {
});
```

The default REST handler is a middleware that formats the data retrieved by the service as JSON. REST handling will be set up automatically when calling `var app = feathers()`. If you would like to configure the REST provider yourself, call `var app = feathers({ rest: false });`.

Then you can configure it manually and add your own `handler` middleware that, for example just renders plain text with the todo description (`res.data` contains the data returned by the service):
The default REST handler is a middleware that formats the data retrieved by the service as JSON. If you would like to configure your own `handler` middleware just pass it to `feathers.rest(handler)`. For example a middleware that just renders plain text with the todo description (`res.data` contains the data returned by the service):

```js
var app = feathers({ rest: false });

app.use(feathers.urlencoded()).use(feathers.json())
.configure(feathers.rest(function restFormatter(req, res) {
app.configure(feathers.rest(function restFormatter(req, res) {
res.format({
'text/plain': function() {
res.end('The todo is: ' + res.data.description);
Expand All @@ -51,8 +46,6 @@ app.use(feathers.urlencoded()).us 527E e(feathers.json())
});
```

__Note:__ When configuring REST this way, you *have* to add `app.use(feathers.urlencoded()).use(feathers.json())` to support request body parsing.

If you want to add other middleware *before* the REST handler, simply call `app.use(middleware)` before configuring the handler.

### SocketIO
Expand Down
29 changes: 14 additions & 15 deletions test/application.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,15 @@ var fs = require('fs');

var feathers = require('../lib/feathers');

describe("Express application", function() {

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

app.use('/path', child);
assert.equal(child.route, '/path');
assert.equal(child.parent, app);
});

});


describe('Feathers application', function () {
it('registers service and looks it up with and without leading and trailing slashes', function () {
var dummyService = {
find: function () {
Expand Down Expand Up @@ -64,7 +59,7 @@ describe('Feathers application', function () {
});
});

it('adds REST by default and registers SocketIO provider', function (done) {
it('adds REST and SocketIO provider', function (done) {
var todoService = {
get: function (name, params, callback) {
callback(null, {
Expand All @@ -77,9 +72,11 @@ describe('Feathers application', function () {
var oldlog = console.log;
console.log = function () {};

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

Expand Down Expand Up @@ -116,9 +113,11 @@ describe('Feathers application', function () {

var oldlog = console.log;
console.log = function () {};
var app = feathers().configure(feathers.socketio(function(io) {
io.set('log level', 0);
})).use('/secureTodos', todoService);
var app = feathers()
.configure(feathers.rest())
.configure(feathers.socketio(function(io) {
io.set('log level', 0);
})).use('/secureTodos', todoService);

var httpsServer = https.createServer({
key: fs.readFileSync(__dirname + '/resources/privatekey.pem'),
Expand Down
11 changes: 4 additions & 7 deletions test/providers/rest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('REST provider', function () {
var server, app;

before(function () {
app = feathers().use('todo', todoService);
app = feathers().configure(feathers.rest()).use('todo', todoService);
server = app.listen(4777);
});

Expand Down Expand Up @@ -117,7 +117,7 @@ describe('REST provider', function () {
}
};

var server = feathers()
var server = feathers().configure(feathers.rest())
.use(function (req, res, next) {
assert.ok(req.feathers, 'Feathers object initialized');
req.feathers.test = 'Happy';
Expand All @@ -142,7 +142,7 @@ describe('REST provider', function () {
});

it('throws a 405 for undefined service methods', function (done) {
var app = feathers().use('todo', {
var app = feathers().configure(feathers.rest()).use('todo', {
get: function (id, params, callback) {
callback(null, { description: 'You have to do ' + id });
}
Expand Down Expand Up @@ -194,14 +194,12 @@ describe('REST provider', function () {

it('Lets you configure your own middleware before the handler (#40)', function(done) {
var data = { description: 'Do dishes!', id: 'dishes' };
var app = feathers({ rest: false });
var app = feathers();

app.use(function defaultContentTypeMiddleware (req, res, next) {
req.headers['content-type'] = req.headers['content-type'] || 'application/json';
next();
})
.use(feathers.urlencoded())
.use(feathers.json())
.configure(feathers.rest())
.use('/todo', {
create: function (data, params, callback) {
Expand All @@ -213,7 +211,6 @@ describe('REST provider', function () {
request({
method: 'POST',
url: 'http://localhost:4775/todo',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
}, function (error, response, body) {
assert.deepEqual(JSON.parse(body), data);
Expand Down
0