首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使meteor restful api/web-service

使meteor restful api/web-service
EN

Stack Overflow用户
提问于 2017-08-18 08:56:06
回答 3查看 847关注 0票数 1

我已经在我的应用程序中创建了一个新的网址/路径,在那里我需要编写一个网络服务。我需要编写一个根据服务中传递的参数删除用户的服务。现在,任何人都应该能够调用该服务(将在稍后阶段使其安全)。App是建立在流星上的。

我的网址是: loaclhost:3000/deleteUser。现在应该可以调用这个页面上定义的删除用户函数,并将json结构数据作为参数传递给它。如果数据有效,则应删除用户。

使用简单:rest包

代码语言:javascript
复制
Meteor.publish("delUser", function (a, b) {
UserDetails.remove({});    //delete user according to data received
}, {
 url: "/testing/delUser",        //url where third party will call the function
   getArgsFromRequest: function (request) {
 // Let's say we want this function to accept a form-encoded request 
 // with fields named `a` and `b`.
console.log('received : ' + JSON.stringify(request.body) );
var content = request.body;

// Since form enconding doesn't distinguish numbers and strings, we need
// to parse it manually
return [content.a, content.b];
}
})

如何访问函数,delUser从一个方?我还需要在稍后阶段添加身份验证。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-03-15 11:07:14

偶发铁:路由器提供服务器端路由,您可以在其中构建自己的函数和api调用。http://iron-meteor.github.io/iron-router/#restful-routes

示例(服务器端代码):

代码语言:javascript
复制
Router.map(function () {
    this.route("api", {path: "/api/:paramsYouNeed",
    where: "server",
    action: function(){
        this.response.writeHead(200, {
          'Content-Type': 'application/json',
          'Access-Control-Allow-Origin': '*'
        });

        if (this.request.method == 'POST') {
            var response;
            //do whatever you want to do                
            this.response.end(response);
        }
    }
});

另一个用户可以通过向上面的url发出一个http.post请求(http:www.a*a.com/api/params)来调用它。

票数 1
EN

Stack Overflow用户

发布于 2017-08-18 15:30:58

就个人而言,我用这个:

简单:休息 简单:json-路线 简单:rest-帐户-密码

我觉得更容易实现。

票数 2
EN

Stack Overflow用户

发布于 2017-08-18 09:57:20

最简单的方法是使用restivus包。

https://atmospherejs.com/nimble/restivus

Restivus使在Meteor 0.9.0+中构建REST API比以往任何时候都更容易!该包受RestStop2和Collection API的启发,构建在简单的JSON路由之上,提供:

  • 创建REST的简单接口
  • Mongo集合CRUD端点的简易设置
  • 通过API进行用户身份验证
  • 可选的登录和注销端点
  • 访问经过身份验证的端点中的this.user
  • 自定义身份验证(如果需要)
  • 限制对特定端点的访问的角色权限
  • 与alanning:roles包一起工作-Meteor接受的角色权限包
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45752571

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档