使流星恢复api / web-service

时间:2017-08-18 08:56:07

标签: rest web-services http meteor

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

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

使用简单:休息包

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?我还需要在以后添加身份验证。

3 个答案:

答案 0 :(得分:2)

个人,我用这个:

  

简单:其余

     

简单:JSON-路由

     

简单:其余账户密码

我发现它更容易实现。

答案 1 :(得分:1)

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

https://atmospherejs.com/nimble/restivus

  

Restivus使得在Meteor 0.9.0+中构建REST API变得前所未有的简单   之前!该软件包的灵感来自RestStop2和Collection API,以及   基于Simple JSON Routes构建,提供:

  • 用于创建REST API的简单界面
  • 轻松设置Mongo集合的CRUD端点
  • 通过API进行用户身份验证
  • 可选的登录和注销端点
  • 访问经过身份验证的端点中的this.user
  • 根据需要自定义身份验证
  • 限制对特定端点的访问的角色权限
  • 与alanning:roles包一起使用 - Meteor的接受角色权限包

答案 2 :(得分:1)

甚至铁:路由器附带服务器端路由,您可以在其中构建自己的功能和api呼叫。 http://iron-meteor.github.io/iron-router/#restful-routes

示例(服务器端代码):

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

其他用户可以通过向上述网址发出http.post请求来调用此网址(http:www.a **** a.com/api/params)

相关问题