【问题标题】:Restivus does not set up endpoint Meteor 1.4Restivus 没有设置端点 Meteor 1.4
【发布时间】:2016-09-02 08:28:29
【问题描述】:

我在使用 Restivus 的 Meteor 1.4.1 应用程序中创建 API 端点时遇到问题。

这是我当前的代码:

import { Restivus } from 'meteor/nimble:restivus'
import { Stripe } from '../stripe/stripe'

export const loadApi = function(){
    console.log('loading api')
    var Api = new Restivus({
        enableCors: true, 
        prettyJson:true
    })

    Api.addRoute('/charge', {
        post: {
            action: function(){
                console.log('* Charging customer')
                console.log(this)
                console.log(this.bodyparams)
                Stripe.chargeCustomer('foobar',9900)
                return {
                    statusCode:204,
                    body:'foobar'
                }
            }
        }
    })
console.log(Api)
}

然后我在 server/main.js Meteor.startup 函数上加载这段代码。 API 似乎已正确定义,因为 console.log 使用正确的 _routes 属性打印 Restivus 对象。

但是,向http://localhost:3000/api/charge 发送post 请求不会触发该功能(console.log 不会执行)。

知道我的代码有什么问题吗?我使用 Meteor 1.4.1,所以它可能是一个错误。

【问题讨论】:

    标签: javascript curl meteor postman restivus


    【解决方案1】:

    尝试从您的路线中删除前导斜杠,因此使用“charge”而不是“/charge”。

    Relevant issue on GitHub.

    【讨论】:

    • @EricBurel 我正在以完全相同的方式进行操作,但仍然无法获得我的 api 的 GET 和 POST 的响应。我还使用 FlowRouter 进行仪表板路由,它不会影响 API 路由正确吗?就像我在流星 1.0 中所做的那样,当时它运行良好。那么是因为流星1.4吗?有什么想法吗?
    • @Shrinath 你能给我们看看代码吗? FlowRouter 应该与此无关(除非您使用它的 SSR 分支?)
    【解决方案2】:

    我没有使用restivus,但是在你的代码中,应该是Api.addRoute('/api/charge'而不是Api.addRoute('/charge'

    【讨论】:

    • 嗨,根据文档,Restivus 不会自行添加 /api/ 前缀。无论如何,我仍然尝试请求/charge,但它也不起作用。
    • 尝试请求/api/charge 而不仅仅是/charge 怎么样?
    • /api/charge 是正确的端点,但我必须在我的代码中将其设置为charge 而不是/charge(Restivus 文档示例具有误导性),接受的答案给出了一个链接到相应的 Github 问题。
    猜你喜欢
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 2015-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 2016-06-21
    相关资源
    最近更新 更多