【问题标题】:Configuring Restivus POST method for Meteor为 Meteor 配置 Restivus POST 方法
【发布时间】:2015-08-15 09:37:35
【问题描述】:

我有以下 Restivus 配置:

if(Meteor.isServer){

    Restivus.configure({
    });

    //Allow Restivus to manage Reports
    Restivus.addCollection('reports');

    Restivus.addRoute('newReport/:message', {}, {

        // POST
        post: {
            action: function(){

                var response = null;
                var message = this.urlParams.message;

                if(message){
                    console.log("Message received: " + message);
                    return {status: "success", data: message};
                } else {
                    console.log("Message empty...");
                    return {status: "fail", message: "Post not found"};
                }

                //Response to caller
                return;
            }
        }
    })

}

按照 Restivus 的解释,当我对 http://localhost:3000/api/newReport/ 进行 GET 调用时,我应该在调用者上从服务器获得“Get All”结果。

但是,如果我在命令行上使用curl -X GET http://localhost:3000/api/newReport/,我似乎会在 api/NewReport/ 处获取站点的 HTML 代码(除了标题和空正文之外,它是空的)

知道了这一点,我知道我的错误出在 Restivus Route 配置中,但我无法查明原因。

预期的行为是,当我从 Ruby 脚本进行 POST 时,我应该收到一条返回的消息(Ok 或 Fail),并且在我的 Meteor 控制台中,我应该看到“收到消息”或“找不到帖子” (两个占位符)。

附加问题,有没有办法禁用 Restivus 在添加集合时创建的默认 GET 方法?

【问题讨论】:

  • 如果您按照 Michael 下面的建议进行操作,您将在 /api/reports 定义一个“获取所有”端点。您从未在api/newReport(仅api/newReport/:message)设置任何路线,因此您收到的是默认的流星模板。你的另一个问题的答案是肯定的。在此处查看文档:github.com/kahmali/meteor-restivus#excludedendpoints

标签: rest http curl meteor routing


【解决方案1】:

您必须在 JavaScript 部分创建一个变量并在 Restivus.addCollection() 调用中使用它。

Reports = Mongo.Collection('reports')

if(Meteor.isServer){

    Restivus.configure({
    });

    //Allow Restivus to manage Reports
    Restivus.addCollection(Reports);
...

【讨论】:

  • 我之前试过了,还是不行。我最终没有使用 Restivus 进行 REST 操作,但谢谢。
  • 这是问题的一半。我在上面留下了评论,解释了问题的其余部分。感谢您提供此信息!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-21
  • 2016-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多