node.js - 添加服务的自定义端点 ( Feathersjs )

标签 node.js api feathersjs


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

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

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

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

最佳答案

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

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

如果您还没有应用程序,请运行此命令来创建一个应用程序:feathers generated 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));

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';
  }
};

现在,如果您向 /custom-organizations 端点发送 get 请求,那么您应该获得测试数据

希望有帮助。

写了一篇关于它的文章 here .

关于node.js - 添加服务的自定义端点 ( Feathersjs ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58927949/

相关文章:

javascript - Feathersjs:如何发送响应并随后触发函数?

使用 Feathers-authentication-client 时出现 Aurelia 依赖问题

javascript - 如何编写代码来测试 websocket 事件?

node.js - 每当我在更改映射组件后重新获取 react 查询时,下一个组件的更改状态显示为 "success"

javascript - Node + PhantomJS | page.evaluate后返回

json - 无法让 xlsx 到 JSON 转换器在 Node/Express 中正常工作

rest - 为什么在简单的 API Controller 中不允许使用 DELETE 方法

python - LibreOffice LightProof 语法库可以对外使用吗?

node.js - 类型错误 : fsevents is not a constructor (already tried a bunch of answers)

php - 尝试使用 bit.ly api 批量缩短网址