当纯粹在节点下运行时,一切都运行正常,一旦我尝试在IIS节点下运行,每次使用getaddrinfo上的ENOTFOUND时,Node http请求都会失败。这是中型网站的一部分,其他所有内容都在Node和IIS Node下工作,只是对Azure CORS Web服务的请求失败。我也有类似的网络服务请求谷歌地图,在两种环境下都能很好地工作。 Google地图和Azure网络服务调用都使用相同的共享代码来处理请求。
// Simplified
//==================================
var http = require("http")
var options = {
host: "xxx.azurewebsites.net",
path: "/address?postcode=SL6+1PB",
headers: {
"SerialNo": "xxx",
"RegKey": "xxx",
"Company": "xxx"
}
};
req = https.request(options, callback);
req.end();
// The Whole thing
//===========================
function getData(host, port, path, headers, data, secure) {
return new promise(function(resolve, reject) {
var options = {}, req;
try {
options.host = host;
if(!!port) {options.port = port;}
if(!!data) {options.method = "POST";}
if(!!path) {options.path = path.replace(/\s+/g, "+");}
if(!!headers) {options.headers = headers;}
if(!!secure) {req = https.request(options, callback);}
else {req = http.request(options, callback);}
req.on("error", function(error) {
rejectPromise(reject, error, "datahttp.getData");
});
if(!!data) {req.write((typeof(data) === "object" ? JSON.stringify(data) : data));}
req.end();
}
catch(error) {
// This error object drops the message property on stringify
error = {method: "datahttp.getData", success: false, message: error.message};
rejectPromise(reject, error, "datahttp.getData");
}
function callback(response) {
var str = "";
//another chunk of data has been received, so append it to `str`
response.on("data", function(chunk) {
str += String(chunk);
});
// the whole response has been received
response.on("end", function () {
if(response.statusCode == 200) {
try {
switch(response.headers["content-type"].split(";")[0]) {
case "application/xml":
xmlToJSN(str)
.then(function(result) {
resolve({method: "datahttp.getData", success: true, data: result});
})
.catch(function(error) {
rejectPromise(reject, error, "datahttp.getData");
});
break;
case "application/json":
resolve({method: "datahttp.getData", success: true, data: JSON.parse(str)});
break;
default:
resolve({method: "datahttp.getData", success: true, data: str});
}
}
catch(error) {
rejectPromise(reject, error, "datahttp.getData");
}
}
else {
rejectPromise(reject, {message: response.statusMessage + " (" + response.statusCode + ")"}, "datahttp.getData");
}
});
response.on("error", function(error) {
rejectPromise(reject, error, "datahttp.getData");
});
}
});
}
我一直在为此奋斗几天,显然只是没有看到一些非常明显的东西,一些帮助和一个上升的头部将受到欢迎
Stack Trace
====================
Trace: { [Error: getaddrinfo ENOTFOUND daaddressing-prod-ne.azurewebsites.net/ daaddressing-prod-ne.azurewebsites.net/:80]
code: 'ENOTFOUND',
errno: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'daaddressing-prod-ne.azurewebsites.net/',
host: 'daaddressing-prod-ne.azurewebsites.net/',
port: 80 }
at ClientRequest.<anonymous> (C:\Dev\Node\acinet_flint\server\datahttp.js:47:29)
at emitOne (events.js:77:13)
at ClientRequest.emit (events.js:169:7)
at Socket.socketErrorListener (_http_client.js:269:9)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at connectErrorNT (net.js:1012:8)
at nextTickCallbackWith2Args (node.js:442:9)
at process._tickCallback (node.js:356:17)
{ [Error: getaddrinfo ENOTFOUND daaddressing-prod-ne.azurewebsites.net/ daaddressing-prod-ne.azurewebsites.net/:80]
code: 'ENOTFOUND',
errno: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'daaddressing-prod-ne.azurewebsites.net/',
host: 'daaddressing-prod-ne.azurewebsites.net/',
port: 80,
service: 'acinet',
success: false,
method: 'datahttp.getData' }