Cập nhật - Được trả lời bởi tự Tôi thấy người ta phải đảm bảo rằng DNS được giải quyết đúng từ máy, hãy kiểm tra mã này để đảm bảo rằng url có thể truy cập nodejs.org/ docs/mới nhất/api/dns.html # dns.resolvenodejs httprequest với dữ liệu - nhận được lỗi getaddrinfo ENOENT
Câu hỏi gốc
tôi đang viết một nút chương trình dựa, trong đó người dùng có thể hỏi tôi để làm một HttpRequest thay mặt họ {tắt khóa học họ cung cấp cho tôi một số dữ liệu và phương thức để gọi với} nhưng mỗi khi tôi thực hiện một httprequest, nó cho tôi một lỗi
getaddrinfo ENOENT đây là cách mã của tôi trông
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…');
}
Để sử dụng phát triển cục bộ 127.0.0.1 thay vì localhost –