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

Best practice for Express middle-ware and other things #313

Closed
kokujin opened this issue Apr 25, 2016 · 5 comments
Closed

Best practice for Express middle-ware and other things #313

kokujin opened this issue Apr 25, 2016 · 5 comments

Comments

@kokujin
Copy link

kokujin commented Apr 25, 2016

I am slightly confused, please bear with me.

  • Services only or Express middle-ware and Services? What is preferred or when should I choose one method over the other? ( Taken from the docs)
  • What is the preferred way to register services

Taken from a scaffolded app

// Connect to the db, create and register a Feathers service.
app.use('/messages', service({
  Model: db,
  paginate: {
    default: 2,
    max: 4
  }
}));

or, from the docs....

// Register your message service
app.configure('/api/messages', memory());
  • According to the docs, Feathers uses Express compatible middle-ware. But the docs show examples using promises, something non Express standard. Why?

Thanks

@daffl
Copy link
Member

daffl commented Apr 26, 2016

Ah I can see how that is confusing, app.configure('/api/messages', memory()); was definitely wrong in the documentation and has been fixed. It is always app.use('/api/messages', memory());.

However, even with the REST API provider configured, passing a service object to app.use will behave different than just passing middleware. Think of a service as its own router that sets up all the routes that you normally would (e.g. GET /users, GET/users/:id, POST /users etc) automatically and maps them to the services method (find, get, create etc.). So in the context of a HTTP REST API, a service is basically just a collection of middleware with its own formatter and you can add your own middleware before or after.

Although Feathers is fully compatible with Express and its middleware, services have a major advantage though: They are protocol independent and also work via websockets and possibly other protocols in the future. You can also use them on the client. In the Philosophy section we outlined the reasons why we think that is important and the Feathers + Express & Socket.io discusses a little more how both play together.

To answer the question which to use when: Use Services and hooks whenever possible. You can still use Express middleware for the parts of the app that are used via a HTTP REST API but unlike services, Express middleware does not work with websockets.

@kokujin
Copy link
Author

kokujin commented Apr 26, 2016

That helped @daffl , Thanks. One last thing, for now.

  • Promises in middle-ware

Feathers uses Express compatible middle-ware. But the docs show examples using promises, something non Express standard. Why? Express uses res.send, res.render or just next() in the case of middleware

@daffl
Copy link
Member

daffl commented Apr 26, 2016

You can use the legacy callback in services and hooks support a legacy next callback but Feathers always prefers promises. Promises are a part of Node now and make working with asynchronous code much easier to avoid callback hell. They also will work with ES2016 async and await which will become part of Node in the future.

Lastly, everything you mentioned is very Express specific. While Feathers currently only works with Express on the server there are plans to also make it more decoupled and compatible with other server frameworks like Koa and Hapi (see #258).

@daffl
Copy link
Member

daffl commented Apr 28, 2016

Closing this one as well. Hope that answered all your questions.

@lock
Copy link

lock bot commented Feb 7, 2019

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue with a link to this issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Feb 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants