2017-09-01 6 views
0

自動更新プロシージャのバージョン番号を提供するためにリモートJSONを取得しています。オンラインであればファイルをプルできるだけで、ネットワークアダプタを無効にして接続を強制しないでください。アプリはデスクトップにクラッシュします。ここでノードWebkit httpクラッシュ

が私のコードです:

var http = require('http'); 
var options = { 
    host: 'xxxx.xxxx.io', 
    path: '/remote.json' 
}; 

var req = http.get(options, function (res) { 
var response = ""; 

    res.on("data", function (data) { 
     response += data; 
     var json = response; 
     console.log("output:\n" + response); 
    }); 
    res.on("error", function (e) { 
     response += e; 
     console.log(e); 
    }); 
    res.on("uncaughtException", function (err) { 
     response += err; 
     console.log(err); 
    }); 
    res.on("end", function() { 
     console.log("end:\n" + response); 
    }); 
}); 

私はいくつかの方法で間違ってエラーチェックを設定しています私の経験不足で思いますか?起動時にアプリがクラッシュしないように、皆さんから回答をいただけますか?

答えて

0

私はこれに対する答えを見つけました。

http.get機能の外でon.errorを宣言する必要がありました。

var http = require('http'); 
var options = { 
    host: 'xxx.xxxxx.io', 
    path: '/remote.json', 
    method: 'get' 
}; 

var res = http.get(options, function (res) { 
    var response = ""; 

    res.on("data", function (data) { 
    response += data; 
}); 
res.on("end", function() { 
    console.log("output:\n" + response); 
    var json = JSON.parse(response); 
    console.log(json.type); 
    }); 
}) 

.setTimeout(3000, function() { 
    console.log("ERROR: The connection timed out."); 
    res.abort(); 
}) 

.on('error', (e) => { 
    if (e.code === "ENOENT") { 
    console.log("ERROR: There is no Internet connection."); 
    } 
}); 
関連する問題