更新 - 通过自我回复 我看到一个人必须确保从计算机上正确解析DNS,检查此代码以确保url可到达nodejs.org/文档/最新/ API/dns.html#dns.resolvenodejs httprequest与数据 - 获取错误getaddrinfo ENOENT
原始的问题
我写的基于程序的节点,其中,用户可以要求我做一个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…');
}
对于本地开发,请使用127.0.0.1而不是本地主机 – 2016-06-23 06:00:04