Search code examples
node.jsfeathersjs

How to use feathersjs to declare internal services without restful?


FeathersJS provides a good way to create restful API on top of nodejs. But I have not figured out a way to create a service internally used in my app without restful.

Below is a sample of code:

const myService = {
  find(params [, callback]) {},
  get(id, params [, callback]) {},
  create(data, params [, callback]) {},
  update(id, data, params [, callback]) {},
  patch(id, data, params [, callback]) {},
  remove(id, params [, callback]) {},
  setup(app, path) {}
}

app.use('/my-service', myService);

In other services, they can use app.service('/my-service') to reference that service instance. But it also exposes a restful API on myService instance. How can I create a service without exposing? I just want to use that service inside my app.


Solution

  • You can use the disallow hook with the external (or rest or socketio) parameter to disable external access:

    const { disallow } = require('feathers-hooks-common');
    const myService = {
      async find(params) {},
      async get(id, params) {},
      async create(data, params) {},
      async update(id, data, params) {},
      async patch(id, data, params) {},
      async remove(id, params) {},
      setup(app, path) {}
    }
    
    app.use('/my-service', myService);
    
    app.service('my-service').hooks({
      before: disallow('external')
    });