在我的node.js应用程序中,我想要进行https api调用。我正在尝试使用https
模块,但它不起作用,但后来我尝试使用request
模块,这很有效。Https请求在node.js中不起作用
不行
var options = {
host : 'myserver/platform-api/v1',
port : 80,
path : '/projects?access_token=38472',
method : 'GET',
headers : {
'Accept' : 'application/json'
}
};
var req = https.request(options, function(res) {
res.on('data', function(chunk) {
console.log(chunk);
});
});
req.on('error', function(e) {
console.log(e);
console.log('problem with request:', e.message);
});
req.end();
我得到这个
problem with request: getaddrinfo ENOTFOUND myserver/platform-api/v1
myserver/platform-api/v1:80
这个作品
request("https://myserver/platform-api/v1/projects?access_token=38472", function(error, response, body) {
if (error) return console.log(error);
//console.log(error);
//console.log(response);
console.log(body);
});
我想不通为什么它不放在了第一位的工作。有谁知道为什么?
感谢
而不是将选项作为参数传递也许传递options.path? –