Node.js getaddrinfo ENOTFound
Node.js를 사용하여 다음 웹 페이지의 HTML 콘텐츠를 가져오려고 할 때:
eternagame.wikia.com/wiki/EteRNA_Dictionary
다음 오류가 발생합니다.
events.js:72
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND
at errnoException (dns.js:37:11)
at Object.onanswer [as oncomplete] (dns.js:124:16)
저는 이미 스택 오버플로에서 이 오류를 찾아냈고, 이것이 node.js가 DNS에서 서버를 찾을 수 없기 때문이라는 것을 깨달았습니다.하지만, 제 코드가 완벽하게 작동하기 때문에 왜 그런지 모르겠습니다.www.google.com.
다음은 제 코드입니다(호스트가 변경된 경우를 제외하고는 거의 유사한 질문에서 복사하여 붙여넣음).
var http = require("http");
var options = {
host: 'eternagame.wikia.com/wiki/EteRNA_Dictionary'
};
http.get(options, function (http_res) {
// initialize the container for our data
var data = "";
// this event fires many times, each time collecting another piece of the response
http_res.on("data", function (chunk) {
// append this chunk to our growing `data` var
data += chunk;
});
// this event fires *one* time, after all the `data` events/chunks have been gathered
http_res.on("end", function () {
// you can use res.send instead of console.log to output via express
console.log(data);
});
});
여기 제가 복사해서 붙여넣은 소스가 있습니다: Expressjs에서 웹 서비스를 호출하는 방법은 무엇입니까?
나는 node.js가 있는 모듈을 사용하지 않습니다.
읽어주셔서 감사합니다.
Node.js에서 HTTP모듈 설명서: http://nodejs.org/api/http.html#http_http_request_options_callback
전화를 걸 수도 있습니다.http.get('http://eternagame.wikia.com/wiki/EteRNA_Dictionary', callback)그런 다음 URL을 구문 분석합니다.url.parse()또는 콜http.get(options, callback),어디에options이라
{
host: 'eternagame.wikia.com',
port: 8080,
path: '/wiki/EteRNA_Dictionary'
}
갱신하다
@Enchanter의 댓글에 명시된 바와 같이.IO, 더port필드 또한 별도의 옵션입니다. 프로토콜http://에 포함되어서는 안 됩니다.host필드. 다른 답변도 사용을 권장합니다.https모듈(SSL이 필요한 경우).
의 다른 일반적인 오류 원인
Error: getaddrinfo ENOTFOUND
at errnoException (dns.js:37:11)
at Object.onanswer [as oncomplete] (dns.js:124:16)
설정할 때 프로토콜(https, https, ...)을 쓰는 중입니다.host의 재산.options
// DON'T WRITE THE `http://`
var options = {
host: 'http://yoururl.com',
path: '/path/to/resource'
};
HTTP 요청에 대한 옵션에서 다음으로 전환합니다.
var options = { host: 'eternagame.wikia.com',
path: '/wiki/EteRNA_Dictionary' };
그게 당신의 문제를 해결해 줄 거라고 생각해요.
문제는 OS X(Mavericks) DNS 서비스를 다시 시작해야 한다는 것이었습니다.Catalina 및 Big Sur DNS 캐시는 다음을 사용하여 지울 수 있습니다.
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
https를 사용해야 하는 경우 https 라이브러리를 사용합니다.
https = require('https');
// options
var options = {
host: 'eternagame.wikia.com',
path: '/wiki/EteRNA_Dictionary'
}
// get
https.get(options, callback);
var http=require('http');
http.get('http://eternagame.wikia.com/wiki/EteRNA_Dictionary', function(res){
var str = '';
console.log('Response is '+res.statusCode);
res.on('data', function (chunk) {
str += chunk;
});
res.on('end', function () {
console.log(str);
});
});
제가 옵션 객체에서 완전한 호스트 URL을 언급했음에도 불구하고 http가 포트 80에서 요청을 한다고 생각합니다.이전에 3000번 포트에서 실행하던 80번 포트에서 API가 있는 서버 애플리케이션을 실행하면 작동했습니다.포트 80에서 응용 프로그램을 실행하려면 루트 권한이 필요합니다.
Error with the request: getaddrinfo EAI_AGAIN localhost:3000:80
다음은 전체 코드 조각입니다.
var http=require('http');
var options = {
protocol:'http:',
host: 'localhost',
port:3000,
path: '/iso/country/Japan',
method:'GET'
};
var callback = function(response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
console.log(str);
});
}
var request=http.request(options, callback);
request.on('error', function(err) {
// handle errors with the request itself
console.error('Error with the request:', err.message);
});
request.end();
나는 이것으로 이 오류를 고쳤습니다.
$ npm info express --verbose
# Error message: npm info retry will retry, error on last attempt: Error: getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:443
$ nslookup registry.npmjs.org
Server: 8.8.8.8
Address: 8.8.8.8#53
Non-authoritative answer:
registry.npmjs.org canonical name = a.sni.fastly.net.
a.sni.fastly.net canonical name = prod.a.sni.global.fastlylb.net.
Name: prod.a.sni.global.fastlylb.net
Address: 151.101.32.162
$ sudo vim /etc/hosts
# Add "151.101.32.162 registry.npmjs.org` to hosts file
$ npm info express --verbose
# Works now!
원본 출처: https://github.com/npm/npm/issues/6686
참조 중인 도메인이 중단된 경우(예: 더 이상 존재하지 않음)에도 이 문제가 발생할 수 있습니다.
나의 경우 오류는 잘못된 호스트 값을 사용했기 때문입니다.
var options = {
host: 'graph.facebook.com/v2.12/',
path: path
}
그래야 한다
var options = {
host: 'graph.facebook.com',
path: path
}
따라서 .com 또는 .net 등 이후의 모든 항목은 경로 매개 변수 값으로 이동해야 합니다.
저의 경우 문제는 잘못된 형식의 URL이었습니다.URL에 이중 슬래시가 있습니다.
저는 요청 모듈을 사용하여 시도했고, 그 페이지의 본문을 꽤 쉽게 인쇄할 수 있었습니다.유감스럽게도 제가 가진 기술로는 그것 외에는 도움이 되지 않습니다.
개발 환경에서 생산 환경으로 이동할 때 이 오류가 발생했습니다.저는 퍼팅에 집착했습니다.https://하지 않기 에 어떤이 될 수 있습니다.이것은 필요하지 않기 때문에 일부에게는 해결책이 될 수 있습니다.
동일한 오류가 발생하여 아래 링크를 사용하여 도움을 받았습니다.
https://nodejs.org/api/http.html#http_http_request_options_callback
내 코드에 포함되지 않았습니다.
req.end();
(NodeJs V: 5.4.0) 위에 추가된 경우req.end();라인, 저는 오류를 제거할 수 있었고 저를 위해 잘 일했습니다.
호스트 이름 대신 서버 IP 주소를 사용해 보십시오.이것은 저에게 효과가 있었습니다.당신에게도 효과가 있기를 바랍니다.
http와 extra slash(/)를 제거했습니다.방금 노드 테스트를 사용했습니다.herokuapp.com ' 그리고 그것은 효과가 있었습니다.
여전히 프록시 설정을 위해 체크아웃을 해야 하는 경우, 저는 프록시 설정이 누락되어 직접 http/https가 차단되어 요청할 수 없었습니다.그래서 저는 요청을 하는 동안 저의 조직에서 프록시를 구성했습니다.
npm install https-proxy-agent
or
npm install http-proxy-agent
const httpsProxyAgent = require('https-proxy-agent');
const agent = new httpsProxyAgent("http://yourorganzation.proxy.url:8080");
const options = {
hostname: 'encrypted.google.com',
port: 443,
path: '/',
method: 'GET',
agent: agent
};
연결 암호에서 원하는 문자가 아닌 문자를 제거하여 이 문제를 해결했습니다.예를 들어, 저는 다음과 같은 문자들을 가지고 있었습니다. <##%> 그리고 그것이 문제를 일으켰습니다(아마도 해시 태그가 문제의 근본 원인이었을 것입니다).
제 문제는 url을 구문 분석하고 http.request()에 대한 http_options를 생성하고 있었다는 것입니다.
이미 도메인 이름의 포트 번호가 있는 request_url.host를 사용하고 있어서 request_url.hostname을 사용해야 했습니다.
var request_url = new URL('http://example.org:4444/path');
var http_options = {};
http_options['hostname'] = request_url.hostname;//We were using request_url.host which includes port number
http_options['port'] = request_url.port;
http_options['path'] = request_url.pathname;
http_options['method'] = 'POST';
http_options['timeout'] = 3000;
http_options['rejectUnauthorized'] = false;
언급URL : https://stackoverflow.com/questions/17690803/node-js-getaddrinfo-enotfound
'programing' 카테고리의 다른 글
| Excel에서 범위 크기를 가져오는 방법 (0) | 2023.05.17 |
|---|---|
| "git push"를 강제로 원격 파일 덮어쓰기 (0) | 2023.05.17 |
| 티켓을 날짜 형식으로 변환하려면 어떻게 해야 합니까? (0) | 2023.05.17 |
| VB.NET 또는 C#에서 iTextsharp dll로 PDF 콘텐츠 읽기 (0) | 2023.05.17 |
| Visual Studio에서 "32비트 선호" 설정의 목적은 무엇이며 실제로 어떻게 작동합니까? (0) | 2023.05.17 |