更新 - 自己回答
我看到必须确保从机器正确解析 DNS,查看节点文档以确保域是可解析的。
原始问题
我正在编写一个基于节点的程序,其中用户可以要求我代表他们执行 httprequest {他们当然会为我提供一些数据和调用方法},但每次我执行 httprequest 它都会给我一个错误
getaddrinfo ENOENT 这就是我的代码的样子
function makehttprequest(deviceid, httpaction, httppath,methods, actiondata, callback) {
console.log('we are here with httpaction' + httpaction + ' path ' + httppath + ' method ' + methods + ' action data ' + actiondata);
//do the http post work, get the data, and call the callback function with return data
var options = {
host: httpaction,
port: 80,
path: httppath,
method: methods
};
try {
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
} catch(e) {
console.log('error as : ' + e.message);
}
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
console.log('writing data to request ..');
req.write(actiondata);
console.log('finished writing data to request…');
req.end();
console.log('request ended…');
}