Feathers: Public / Private services

Created on 8 Oct 2017  ·  4Comments  ·  Source: feathersjs/feathers

Hey guys! First of all, I recently discovered feathersjs and I have to say it's really nice to use.
The way we can expose services with rest or socket is really amazing.
I'm using it to build a REST api for a React frontend, but I'm still a bit lost with a couple of things.

I don't know where to put my "private" services. For example, I have a mailer service, using a translation service to send translated emails to users (when they register, forgot password, ...). This services will never be expose with the rest api, it's purely an internal service. A user will, of course, never be able to send an email by sending a POST to /mailer. But still, it seems that I need to attach my service to a route and that the service exposition is the default behavior. To disable the "external" access to my service, I have to use hooks like hooks.disallow('external') on all method before hook of my service, right ? Am I missing something or do we have a simpler way to define private services ? I also don't like to have all my service (private & public) in the same folder (src/services). Should I define this "private services" a completely different way ? Like, just adding them to the app like this app.set('mailer', mailer) and lose the benefit of service hooks. Can we use the application as a services containers and what is the recommended way to inject services into other services ? Get them from the app ? Like app.service('xxx') ?
Also, when using the cli and generating a service. Every service get a name option. What is this name use for ? When we call app.service(xxx), it resolves by the service attach to the path xxx, right ?

Thank you !

Most helpful comment

All 4 comments

To disable the "external" access to my service, I have to use hooks like hooks.disallow('external')

Yes, you should use something like:

app.use('/mypath', myservice)
app.service('/mypath').hooks({
  before: { all: [hooks.disallow('external')] } 
})

Should I define this "private services" a completely different way ?

My suggestion would be to have like a private namespace and then use a global hook. Whenever you want a private service just name them like so /private/svc1 /private/svc2, then make a global app hook that conditionally applies disallow('external').

You can keep you folder structures however you please, it's only the service name that matters. But I do both. Like all my DB services are in a DB folder and I also name space them like so db/users ...


Like, just adding them to the app like this app.set('mailer', mailer) and lose the benefit of service hooks.

Not a good idea. Keep sticking to app.use()


Can we use the application as a services containers and what is the recommended way to inject services into other services ? Get them from the app ? Like app.service('xxx') ?

The recommended way, and what I also stick to, is to use ES6 Classes for services, in which case you get a setup method that provides the app object.

export default class Service {
 setup(app) {
    this.app = app;
    this.svc2 = app.service('svc2');
  }

  create(data) {
    // use this.svc2 here now 
  }
}

app.use('/service', new Service());

Also, when using the cli and generating a service. Every service get a name option. What is this name use for ? When we call app.service(xxx), it resolves by the service attach to the path xxx, right ?

I don't use cli in my project as it was more of a migration from express than a fresh one. So I'm not fully sure about that aspect. But yes when we call app.service('xyz'), it resolves them by the path you defined when adding the service app.use('xyz', new Xyz())

Thank you @subodhpareek18, that's exactly it 😄

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ausir0726 picture ausir0726  ·  3Comments

perminder-klair picture perminder-klair  ·  3Comments

RickEyre picture RickEyre  ·  4Comments

NetOperatorWibby picture NetOperatorWibby  ·  4Comments

andysay picture andysay  ·  3Comments