2017-09-08 3 views
2

이것은 매우 단순한 것으로 가정되었지만 데이터를 다시 얻는 대신 정의되지 않습니다.Node.JS get get undefined

나는 프록시 서버 뒤에 있으며 프록시 서버 뒤에서 요청을하는 것이 좋지 않습니다. 외부 모듈을 사용하지 않고 프록시 서버를 사용할 때 HTTP 요청을 설명하는 튜토리얼 하나만 발견했는데 제대로 설명하지 못했습니다.

const http = require('http'); 
let options = { 
    host: '10.7.0.1', 
    port:8080, 
    path:'www.google.co.uk' 
} 

http.get(options,(res) => { 
    let str = ''; 
    res.setEncoding('utf8'); 
    res.on('data', (chunk) => { 
    str += chunk; 
}) 

res.on('end', (str) => { 
    console.log(str); 
    }) 
}) 

10.7.0.1은 프록시 서버의 주소이고 8080은 포트입니다.

이 코드의 출력은 정의되지 않습니다.

나는 방법이 정확하고 그것을 알아낼 수 없다는 것을조차 모른다. 나는 노드 http 문서를 읽고 나의 이해의 베스트에 선택권 목표를 썼다, 틀리면 정정하십시오.

그리고 일반적으로 외부 모듈을 사용하지 않고 프록시 서버를 사용할 때 어떻게 요청합니까?

+0

업데이트를보고 나에게 알려주십시오 :) @Daksh – turmuka

답변

0

당신이 HTTP를 사용하여 프록시에 연결하려면이를 참조하십시오, 당신은 연결 방법을 사용할 필요가 중요

const options = { 
    port: 8080, 
    hostname: '10.7.0.1', 
    method: 'CONNECT', 
    path: 'www.google.com:80' 
    }; 

    const req = http.request(options); 
    req.end(); 

    req.on('connect', (res, socket, head) => { 
console.log('got connected!'); 

socket.write('GET/HTTP/1.1\r\n' + 
      'Host: www.google.com:80\r\n' + 
      'Connection: close\r\n' + 
      '\r\n'); 
socket.on('data', (chunk) => { 
    console.log(chunk.toString()); 
}); 
socket.on('end',() => { 
    proxy.close(); 
} 

추가 정보 (필요하지)

다음은 프록시를 만들고 요청을 만드는 방법에 대해 here에서 발견 된 전체 코드입니다. 그것은 기본적으로 서버를 만들고 그 위에 요청합니다.

const http = require('http'); 
const net = require('net'); 
const url = require('url'); 

// Create an HTTP tunneling proxy 
const proxy = http.createServer((req, res) => { 
    res.writeHead(200, { 'Content-Type': 'text/plain' }); 
    res.end('okay'); 
}); 
proxy.on('connect', (req, cltSocket, head) => { 
    // connect to an origin server 
    const srvUrl = url.parse(`http://${req.url}`); 
    const srvSocket = net.connect(srvUrl.port, srvUrl.hostname,() => { 
    cltSocket.write('HTTP/1.1 200 Connection Established\r\n' + 
        'Proxy-agent: Node.js-Proxy\r\n' + 
        '\r\n'); 
    srvSocket.write(head); 
    srvSocket.pipe(cltSocket); 
    cltSocket.pipe(srvSocket); 
    }); 
}); 

// now that proxy is running 
proxy.listen(1337, '127.0.0.1',() => { 

    // make a request to a tunneling proxy 
    const options = { 
    port: 1337, 
    hostname: '127.0.0.1', 
    method: 'CONNECT', 
    path: 'www.google.com:80' 
    }; 

    const req = http.request(options); 
    req.end(); 

    req.on('connect', (res, socket, head) => { 
    console.log('got connected!'); 

    // make a request over an HTTP tunnel 
    socket.write('GET/HTTP/1.1\r\n' + 
       'Host: www.google.com:80\r\n' + 
       'Connection: close\r\n' + 
       '\r\n'); 
    socket.on('data', (chunk) => { 
     console.log(chunk.toString()); 
    }); 
    socket.on('end',() => { 
     proxy.close(); 
    }); 
    }); 
}); 
그들이 완료 후 http.createServer()를 통해 서버가 다음 proxy.listen()와 듣기 시작 만드는

, 그것은 HTTP의 connect 방법을 사용하여 연결하고 소켓 간단한 GET 요청에 기록하고, 그것이 구글로 전송 지정된 경로.

+0

고마워요.하지만 외부 모듈을 사용하여 요청을 만드는 방법을 설명한 Google 자습서가 많이 있지만 Node의 표준 모듈을 사용하여 동일한 작업을 수행하려고합니다. – Daksh

+0

http 모듈을 사용하여 프록시 뒤에 요청을하려면 해당 명령을 사용하여 프록시를 설정해야합니다. @Daksh – turmuka

+0

이미 npm 내에 프록시를 설정했습니다. – Daksh