리그캣의 개발놀이터

[node.js] node에서 get, post 요청할때 undefined 현상 본문

개발 공부/Node.js(웹)

[node.js] node에서 get, post 요청할때 undefined 현상

리그캣 2020. 4. 8. 01:07

node에서 post 나 get 구축 말고, 다른 서버에 get이나 post 요청하는 방법은 아래와 같다.

var request = require('request');
var options = {
  'method': 'GET',
  'url': 'ip',
  'headers': {
    'Content-Type': 'application/json',
  }
};
request(options, function (error, response) { 
//   if (error) throw new Error(error);
  console.log(response);
});

코드는 대략 위와 같고 method를 'get' or 'post'로 변경해주면 된다.

이때 나는 아래

consol.log(response)

를 찍어보니 undfined가 나왔고 해결하기 위해 err로그를 찍어보았다.

Error: Invalid protocol: ip:

내가 지정한 ip인 예를들어 102.11.11.11의 프로토콜이 올바르지 않다고 나왔고 알고보니 맨앞에 schme인 http:// or https://를 선언해 주어야했다

즉 ip = 102.11.11.111이 아닌 http://102.11.11.111로 선언해주니 해결되었다. 포스트맨에서는 자동으로 schem을 채워주나보다

Comments