0

I have trouble creating an API Endpoint in a Meteor 1.4.1 app using Restivus.

Here is my current code :

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

Then I load this code on the server/main.js Meteor.startup function. The API seems to be correctly defined, as the console.log prints the Restivus object with the correct _routes attribute.

However,sending a post request to http://localhost:3000/api/charge does not trigger the function (the console.log are not executed).

Any idea about what could be wrong with my code ? I use Meteor 1.4.1, so it might be a bug.

Eric Burel
  • 3,790
  • 2
  • 35
  • 56

2 Answers2

2

Try removing the leading slash from your route, so 'charge' instead of '/charge'.

Relevant issue on GitHub.

erikwall
  • 151
  • 4
  • @EricBurel I'm doing exactly the same way but still not able to get the response of GET and POST of my api's. I also using FlowRouter for dashboard route and it will not impact API routing correct ? The same way i did in meteor 1.0 and it was working fine at that time. so is it because of meteor 1.4 ? Any idea ? – Shrinath Jan 06 '17 at 07:03
  • @Shrinath could you show us the code ? FlowRouter should have nothing to do with this indeed (except maybe if you use its SSR branch ?) – Eric Burel Jan 06 '17 at 08:31
0

I have not use restivus, but in your code, should it be Api.addRoute('/api/charge' instead of Api.addRoute('/charge' ?

Khai
  • 91
  • 1
  • 7
  • Hi, no Restivus adds the `/api/` prefix itself according to doc. Anyway I still tried to request `/charge` but it did not work either. – Eric Burel Sep 01 '16 at 19:38
  • What about trying to request `/api/charge` instead of just `/charge`? – Khai Sep 03 '16 at 03:05
  • Yes `/api/charge` is the correct endpoint, but I had to set it up as `charge` instead of `/charge` in my code (the Restivus doc examples are misleading), the accepted answer gives a link to the corresponding Github issue. – Eric Burel Sep 05 '16 at 07:19