如果是在内网用nodejs的http或https去访问外部网站,需要添加proxy,
首先要用npm安装https-proxy-agent,然后执行以下代码
代码块
var HttpsProxyAgent = require('https-proxy-agent')
var path = req.query.path;
var proxy = 'http://user:pass@xxx.com:port';
var agent = new HttpsProxyAgent(proxy);
https_options = {
"host": "api.cognitive.microsoft.com",
"path": path,
"port": 443,
"agent": agent,
"headers": {
"Ocp-Apim-Subscription-Key": "your-Subscription-Key"
}
};
request = https.get(https_options, function(respon) {
var body = '';
respon.on('data', function(chunk) {
body += chunk;
});
respon.on('end', function(){
var d = JSON.parse(body);
res.send(d);
});
}).on('error', function(e) {
res.send(null);
});