6


I am new to NodeJS world. I found FeatherJS is a awesome tools/framework to build API service with very less Coding

I need to add a custom service endpoint (like : localhost/servicename/custom-end-point ). I also need to grab data from user in those end-point (could be a get request or post).

I have already gone through followings links, but nothing is clearly mention there,

https://docs.feathersjs.com/guides/basics/services.html
https://docs.feathersjs.com/api/services.html

coder618
  • 848
  • 1
  • 9
  • 12

1 Answers1

5

Install feathers-cli using the following command: npm install -g @feathersjs/cli.

To create a service, navigate to your project directory and run this command feathers generate service. It will ask some questions like service name.

If you don't already have an app then run this command to create one: feathers generate app.

Thats it!


Update:

Lets assume you have a service named organizations and you want to create a custom endpoint like custom-organization. Now, create a file inside services > organizations named custom-organizations.class.js. Add the following line in your organizations.service.js file.

// Import custom class
const { CustomOrganizations } = require('./custom-organizations.class');

// Initialize custom endpoint    
app.use('/custom-organizations', new CustomOrganizations(options, app));

Add the following code in your custom-organizations.class.js file.

const { Service } = require('feathers-mongoose');
exports.CustomOrganizations = class CustomOrganizations extends Service {
  constructor(options, app) {
    super(options);
  }

  async find() {
    return 'Test data';
  }
};

Now, if you send a get request to /custom-organizations endpoint then you should get Test data.

Hope it helps.

Wrote an article about it here.

Shihab
  • 2,641
  • 3
  • 21
  • 29
  • 1
    Thnaks, I know that, but i need to extra endpoint to a existing service, which will point to a custom service method – coder618 Nov 19 '19 at 06:53
  • 1
    @Ahadul, check the updated answer. I am using latest version of FeathersJS. – Shihab Nov 19 '19 at 07:03
  • Thanks Man, Really helpful. Can i able to CRUD action(of any service) inside custom-organizations.class.jsthis file, can you help me with that? if i use const service = app.service('organizations');code my app crash. – coder618 Nov 19 '19 at 07:41
  • CRUD should be available inside custom-organizations (not sure). `const service = app.service('organizations');` did you mean `custom-organizations`? – Shihab Nov 19 '19 at 07:45
  • Yes i need to do CRUD inside CustomOrganizations Class(file name :custom-organizations.class.js ) . – coder618 Nov 19 '19 at 08:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/202623/discussion-between-ahadul-and-dijkstra). – coder618 Nov 19 '19 at 08:25