使用Meteor扩展缩短的URL

时间:2015-04-10 07:51:58

标签: javascript node.js meteor

我正在尝试在我的Meteor应用程序中扩展缩短的URL,这可能已被多次缩短。 我遵循了Axel Rauschmayer给出的node.js示例:http://www.2ality.com/2012/04/expand-urls.html

他的基本node.js示例如下:

var request = require("request");

function expandUrl(shortUrl) {
    request( { method: "HEAD", url: shortUrl, followAllRedirects: true },
        function (error, response) {
            console.log(response.request.href);
        });
}

我尝试使用Meteor的http包重新实现它(我相信它是基于上面使用的'request'节点包构建的。)

我试图在Meteor中使用goo.gl网址进行此操作,该网址会转到流媒体subreddit的bit.ly网址,如下所示:

HTTP.call("HEAD", 'http://bit.ly/1adkwWO',{},
        function (error, result){
            if (!error){
                console.log("result: " + JSON.stringify(result));
                console.log("statusCode: " + result.statusCode);
                console.log("data: " + result.data);
                console.log("headers: " + JSON.stringify(result.headers));
                console.log("href: " + result.request.href);

            }else {
                console.log(error);
            }
        });

并生成一个如下所示的控制台日志:

I20150410-08:43:10.961(1)? statusCode: 200
I20150410-08:43:10.961(1)? data: null
I20150410-08:43:10.961(1)? headers: {"date":"Fri, 10 Apr 2015 07:43:10 GMT","content-type":"text/html; charset=UTF-8","connection":"keep-alive","set-cookie":["__cfduid=d1c9e25e3211f8c83df44b779033c8cc81428651790; expires=Sat, 09-Apr-16 07:43:10 GMT; path=/; domain=.reddit.com; HttpOnly"],"x-ua-compatible":"IE=edge","x-frame-options":"SAMEORIGIN","x-content-type-options":"nosniff","x-xss-protection":"1; mode=block","vary":"accept-encoding","x-moose":"majestic","cache-control":"no-cache","cf-cache-status":"MISS","server":"cloudflare-nginx","cf-ray":"1d4cd1b8b5c81347-LHR"}
I20150410-08:43:10.974(1)? Exception in callback of async function: TypeError: Cannot read property 'href' of undefined
I20150410-08:43:10.974(1)?     at app/twittest.js:91:58
I20150410-08:43:10.974(1)?     at runWithEnvironment (packages/meteor/dynamics_nodejs.js:108:1)

我可以看到我从reddit获取响应,因为域名'reddit.com'出现在标题中,但由于响应对象不存在,我无法检索URL。

有没有明显的我做错了,自上面发布的示例后请求模块是否已更改?

感谢您寻找

0 个答案:

没有答案