我是 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/