[NODE] 서버에서 다른 웹 사이트의 데이터를 가져와 응답하기

2022. 1. 25. 16:51NodeJs

반응형
var http = require('http');
var opts = {
    host: 'www.google.com',
    port:80,
    method:'post',
    path:'/',
    headers:{}
}
 
var resData = '';
var req = http.request(opts, function(res){
    // 응답 처리
    res.on('data',function(chunk){
        resData += chunk;
    });
 
    res.on('end', function(){
        console.log(resData);
    })
});
 
opts.headers['Content-Type'] = 'application/x-www-form-urlencoded';
req.data = "q=actor";
opts.headers['Content-Length'] = req.data.length;
 
req.on('error', function(err){
    console.log("오류 발생 : " + err.message);
});
 
// 요청 전송
req.write(req.data);
req.end();​

 

참고서적 : DOit! Node.js 프로그래밍 / 정재곤

반응형