简体   繁体   中英

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

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 ithere .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM