使流星恢复api / web-service [英] make meteor restful api/web-service
问题描述
我在我的应用中创建了一个新的网址/路由,我需要编写一个网络服务。我需要编写一个服务,根据服务中传递的参数删除用户。现在,任何人都应该可以调用该服务(将在以后阶段使其安全)。应用程序建立在meteor上。
I have created a new url/route in my app where I need to write a web-service. I need to write a service that deletes user according to the parameters passed in the service. For now, anyone should be able to call that service (will make it secure at later stage). App is built on meteor.
我的网址是:loaclhost:3000 / deleteUser。现在应该能够调用我在此页面上定义的删除用户函数,并将json结构数据作为参数传递给它。如果数据有效,则应删除该用户。
My url is : loaclhost:3000/deleteUser. Now one should be able to call my delete user function defined on this page and pass json structure data as an argument to it. If the data is valid, then the user should be deleted.
使用简单:休息包
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第三次聚会?我还需要在稍后阶段添加身份验证。
How to access the function, delUser from a thrid party? I also need to add authentication at a later stage.
推荐答案
甚至铁:路由器附带服务器端路由,您可以在其中构建自己的功能和api呼叫。
http://iron-meteor.github.io/iron -router / #restful-routes
even iron:router comes with server side routes where you can build your own functions and api calls. http://iron-meteor.github.io/iron-router/#restful-routes
示例(服务器端代码):
Sample (Server side code) :
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)
The other user can call this by making a http.post request to the above url (http:www.a****a.com/api/params)
这篇关于使流星恢复api / web-service的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!