make meteor restful api/web-service

2019-06-11 07:50发布

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.

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.

Using simple:rest package

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

How to access the function, delUser from a thrid party? I also need to add authentication at a later stage.

3条回答
手持菜刀,她持情操
2楼-- · 2019-06-11 08:38

Personnally, I use this :

simple:rest

simple:json-routes

simple:rest-accounts-password

I find it easier to implement.

查看更多
来,给爷笑一个
3楼-- · 2019-06-11 08:48

The easiest way to do this is use the restivus package.

https://atmospherejs.com/nimble/restivus

Restivus makes building REST APIs in Meteor 0.9.0+ easier than ever before! The package is inspired by RestStop2 and Collection API, and is built on top of Simple JSON Routes to provide:

  • A simple interface for creating REST APIs
  • Easy setup of CRUD endpoints for Mongo Collections
  • User authentication via the API
  • Optional login and logout endpoints
  • Access to this.user in authenticated endpoints
  • Custom authentication if needed
  • Role permissions for limiting access to specific endpoints
  • Works alongside the alanning:roles package - Meteor's accepted role permission package
查看更多
混吃等死
4楼-- · 2019-06-11 08:53

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

The other user can call this by making a http.post request to the above url (http:www.a****a.com/api/params)

查看更多
登录 后发表回答