我已经在我的应用程序中创建了一个新的网址/路径,在那里我需要编写一个网络服务。我需要编写一个根据服务中传递的参数删除用户的服务。现在,任何人都应该能够调用该服务(将在稍后阶段使其安全)。App是建立在流星上的。
我的网址是: loaclhost:3000/deleteUser。现在应该可以调用这个页面上定义的删除用户函数,并将json结构数据作为参数传递给它。如果数据有效,则应删除用户。
使用简单:rest包
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从一个方?我还需要在稍后阶段添加身份验证。
发布于 2018-03-15 11:07:14
偶发铁:路由器提供服务器端路由,您可以在其中构建自己的函数和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);
}
}
});另一个用户可以通过向上面的url发出一个http.post请求(http:www.a*a.com/api/params)来调用它。
发布于 2017-08-18 15:30:58
就个人而言,我用这个:
简单:休息 简单:json-路线 简单:rest-帐户-密码
我觉得更容易实现。
发布于 2017-08-18 09:57:20
最简单的方法是使用restivus包。
https://atmospherejs.com/nimble/restivus
Restivus使在Meteor 0.9.0+中构建REST API比以往任何时候都更容易!该包受RestStop2和Collection API的启发,构建在简单的JSON路由之上,提供:
https://stackoverflow.com/questions/45752571
复制相似问题