添加服务的自定义端点 ( Feathersjs )

Aha*_*dul 6 api node.js feathersjs


我是 NodeJS 世界的新手。我发现 FeatherJS 是一个很棒的工具/框架,可以用很少的编码构建 API 服务

我需要添加自定义服务端点(例如: localhost/servicename/custom-end-point )。我还需要从这些端点的用户获取数据(可能是获取请求或发布)。

我已经浏览过以下链接,但那里没有明确提及,

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

Dij*_*tra 5

使用以下命令安装feathers-clinpm install -g @feathersjs/cli:。

要创建服务,请导航到项目目录并运行此命令feathers generate service。它会询问一些问题,例如服务名称。

如果您还没有应用程序,请运行以下命令来创建一个应用程序:feathers generate app

就是这样!


更新:

假设您有一个名为 的服务organizations,并且您想要创建一个自定义端点,例如custom-organization. 现在,在services > Organizations中创建一个名为custom-organizations.class.js. 在您的文件中添加以下行organizations.service.js

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

// Initialize custom endpoint    
app.use('/custom-organizations', new CustomOrganizations(options, app));
Run Code Online (Sandbox Code Playgroud)

在您的文件中添加以下代码custom-organizations.class.js

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

  async find() {
    return 'Test data';
  }
};
Run Code Online (Sandbox Code Playgroud)

现在,如果您向端点发送 get 请求,/custom-organizations那么您应该得到Test data.

希望能帮助到你。

在这里写了一篇关于它的文章。