coder618
coder618

Reputation: 1004

Add Custom Endpoint For Service ( Feathersjs )


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

Upvotes: 7

Views: 3388

Answers (1)

Shihab
Shihab

Reputation: 2679

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.

Upvotes: 6

Related Questions