[SOLVED] Materials for learning how to use the webapp feature in meteor

If you’re trying to set up a few endpoints on your server I really recommend the meteor simple:rest packages. They provide lots of helpful stuff around creating REST endpoints and exposing data through REST instead of DDP like Meteor usually does. These are way easier than using the webapp package yourself.

If you just need to set up endpoints for receiving webhooks I recommend simple:json-routes which is part of the simple:rest suite of packages.

I use this for receiving incoming webhooks from different services within my application. Here’s one basic example I’ve pulled from my app which I use to register invoices from Stripe.


const setupWebhookRoute = () => {
   // i need to enable CORS for this route, so I added the OPTIONS request to allow it.
  // if you don't need CORS this isn't necessary
  JsonRoutes.add('OPTIONS', '/register-stripe-invoice', (req, res, next) => {
    JsonRoutes.sendResult(res, {
      headers: {
        'Access-Control-Allow-Origin': '*', // enable CORS
        'Access-Control-Allow-Methods': 'GET, OPTIONS',
        'Access-Control-Allow-Headers': 'Content-Type, Authorization, X-Requested-With'
      }
    });
  });

  JsonRoutes.add('POST', '/register-stripe-invoice', async function (req, res, next) {
    const webhookPayload = req.body;
    //... now do anything you need to with the webhook payload
 
    const dataResponse = {
      //...put whatever response data you want here (if you need to send a response)
    };
    return JsonRoutes.sendResult(res, {
      data: dataResponse,
      headers: {
        // enable CORS
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'GET, OPTIONS',
        'Access-Control-Allow-Headers': 'Content-Type, Authorization, X-Requested-With'
      }
    });
  });
};

And then at server startup I call setupWebhookRoute(), then I have a new route at POST /register-stripe-invoice.

1 Like