如何设置meteor 以接收来自外部API 的事件? [英] How do I setup meteor to receive events from an external API?

查看:46
本文介绍了如何设置meteor 以接收来自外部API 的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用此外部 API 注册了我的网址(我们将其称为 https://mywebaddress/callbacks),现在它会在完成操作时向我发送 JSON.我不需要向它发起任何出站,我只需要接收 JSON 并存储它.

I have registered my web address (let's just call it https://mywebaddress/callbacks) with this external API and it will now send me JSON when it completes an action. I don't need to initiate anything outbound to it, I just need to receive the JSON and store it.

JSON 数据将通过 POST 接收

JSON data will be receive via POST

推荐答案

Paul 的链接让我找到了正确的方向.(http://www.meteorpedia.com/read/REST_API).

Paul's link sent me in the right direction. (http://www.meteorpedia.com/read/REST_API).

然后我找到了标题为WebApp.connectHandlers and connect"的部分.

Then I found the section titled "WebApp.connectHandlers and connect".

我使用了在那里找到的代码,但在我的实例中,代码中有错误.我不得不将第一行从 var connect = Npm.require('connect'); 更改为 var connect = Meteor.require('connect');

I used the code found there, but in my instance there was an error in the code. I had to change the first line from var connect = Npm.require('connect'); to var connect = Meteor.require('connect');

这是下面的代码.

// necessary to parse POST data
var connect = Meteor.require('connect');
// necessary for Collection use and other wrapped methods
var Fiber = Npm.require('fibers');
WebApp.connectHandlers
    .use(connect.urlencoded())  // these two replace
    .use(connect.json())        // the old bodyParser
    .use('/getUserProfile', function(req, res, next) {

        // necessary for Collection use and other wrapped methods
        Fiber(function() {

            var userId = req.body.userId;
            var user = Meteor.users.findOne(userId);

            res.writeHead(200, {'Content-Type': 'application/json'});
            res.end(JSON.stringify(user.profile));

        }).run();
    });    
}

然后为了测试这是否有效,我使用了 http://www.hurl.it/.我将目的地更改为 POST 并添加了一个内容类型的标头 - application/json.然后我在正文中粘贴了一些我知道来自平衡的 JSON.如果您需要一个工具来查看实际发布到您的服务器的内容,您可以使用 http://requestb.in/.

Then to test that this was working I used http://www.hurl.it/. I changed the destination to POST and added a header of content-type - application/json. I then pasted in the body some JSON that I knew came from balanced. If you need a tool to see what is actually being posted to your server you can use http://requestb.in/.

这篇关于如何设置meteor 以接收来自外部API 的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆