Search code examples
feathersjs

Feathers.js - get service path/route from service instance


Is there a way to get service path/route from the service wrapper?

Something like this:

let myService = app.service('users');

myService.name === 'users'; // true

I am trying to create new services dynamically based on existing services, like so:

let services = [service1, service2, service3];

services.forEach(service=>{
    app.use(`somePrefix-${service.name}`, {get: get, find:find /*etc*/});
});

And I need the path of the service to register for the new path.


Solution

  • There a two ways to do this. The first, you already answere yourself: Use the setup method and set this.path = path.

    The other would be to add a service mixin which will be called whenever a service is registered.

    app.mixins.push((service, path) => {
      // Only do this for non-prefixed services
      if(path.indexOf('somePrefix') === -1) {
        services.forEach(service => {
            app.use(`somePrefix-${path}`, {get: get, find:find /*etc*/});
        });
      }
    });