2017-11-13 9 views
0

Ich versuche, einige alte Knoten js Code zu pflegen. Ich habe Probleme bei der Verbindung zu https URL mit Corporate Proxy.Verbinden Sie https mit nodejs

Unten Code scheint nicht zu funktionieren.

var https = require('https'); 
var options = { 
    hostname: 'PROXY_IP', 
    port : PROXY_PORT, 
    path : 'https://server.ourdomain.com/path/to/secure/api', 
    rejectUnauthorized : false, 
    headers: { 
     'Authorization': 'Basic ' + new Buffer('username:password').toString('base64'), 
     'Content-Type': 'application/json', 
     'Host' : 'https://server.ourdomain.com:443' 
    } 
} 
var responseChunk = ''; 
callback = function(response) { 
    if(response.statusCode !== 200) { 
     //Error occured. Handle error 
    } 
    response.on('data', function (chunk) { 
     responseChunk += chunk; 
    }); 
    response.on('end', function() { 
     // Got complete response. Process and do something 
    }); 
} 
var get_req = https.request(options, callback); 
get_req.on('error', function(e) { 
    console.log("Error:" + e) 
}); 
get_req.end(); 

Fehler nach dieser Ausführung ist

Error:Error: socket hang up 

ich in der Lage war, diese Funktion zu erhalten request Modul. Aber nicht mit https Modul.

Was scheint das Problem zu sein?

Antwort

0

Verwenden Anfrage ..

var request = require('request'); 
request({'url':'https://anysite.you.want/sub/sub','proxy':'http://yourproxy:8087'}, function (error, response, body) { 
if (!error && response.statusCode == 200) { 
console.log(body); 
} 
})