小编Max*_*ali的帖子

如何捕获getaddrinfo ENOTFOUND

我有一个链接列表,我需要在处理一些数据之前检查.使用http.get检查标头会返回错误:

events.js:72   
        throw er; // Unhandled 'error' event    
          ^    
Error: getaddrinfo ENOTFOUND       
    at errnoException (dns.js:37:11) 
Run Code Online (Sandbox Code Playgroud)

我无法处理此错误,并退出该过程.我尝试了res.on("error")并在http.get上尝试了..catch,但没有任何效果.

下面是代码片段,这是runnable.com上的实例

//This is OK
getHeaders('http://google.com/404pag-that-does-not-exit');


//Here is the error.
//Uncoughtable error!
getHeaders('http://doesnotexistooooo.com');

function getHeaders(link){
    var _http = require("http");
    var myUrl = require("url");

    var qs=(myUrl.parse(link).search==null) ? "" : myUrl.parse(link).search ;
    var path=myUrl.parse(link).pathname;

    var options = {
        hostname: myUrl.parse(link).hostname,
        path: path+qs,
        method: 'HEAD'
    };
    _http.get(options, function(res) {
        res.on('error',function(e){
            console.log("Error: " + myUrl.parse(link).hostname + "\n" + e.message); 
            console.log( e.stack );
        });
        console.log('STATUS: ' + res.statusCode);
        console.log('HEADERS: …
Run Code Online (Sandbox Code Playgroud)

dns http node.js getaddrinfo

11
推荐指数
1
解决办法
2万
查看次数

在knockout中从JSON对象创建ko.observableArray

我刚刚开始使用knockout.js,它在正常竞标中效果很好.我有observableArray的问题.

我想创建一个observableArray并为其分配来自Google Feed API的JSON数据.以下是JSON格式https://developers.google.com/feed/v1/devguide#resultJson

google.load("feeds", "1");  // Loads Google Feed API
function FeedViewModel()
{
    // Data
    var self = this;
    self.allEntries = null;

    // Example property, and it works
    self.feedHead = ko.observable("BBC News");

    var feed = new google.feeds.Feed("feeds.feedburner.com/BBCNews");
    feed.setResultFormat(google.feeds.Feed.JSON_FORMAT);
    feed.includeHistoricalEntries();
    feed.setNumEntries(30);

    // Loads feed results
    feed.load(function (result) {
        if (!result.error) {           
            self.allEntries = ko.observableArray(result.feed.entries);

            // accessing the title from here is OK
            alert(self.allEntries()[1].title);
        }        
    });
}
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,从ViewModel访问数组是正常的,但是我需要使用foreach在浏览器中显示它:allEntries

<h2 data-bind="text: feedHead">Latest News</h2>
<!-- ko foreach:allEntries -->
    <div class="lists">
        <a href="#" …
Run Code Online (Sandbox Code Playgroud)

javascript json google-feed-api knockout.js

5
推荐指数
1
解决办法
2万
查看次数