1

My code works fine on my local machine because I have added an entry in the etc.hosts file to do the DNS translation. But the moment I put this in my cloud vendor it fails for getaddrinfo ENOTFOUND.can anyone help me on how to fix this. This is not a duplicate of other question. The other one uses https.get but mine uses the post method and the npm library "request"

request = require('request'),     
var headers = {
    'Authorization': 'Basic MTk3Y2RkY2UwYTBjNDA1NGIyN6MGQyOGRiOWYtOGUwYS00M2ExLWEyMzEtYWY4NTgzZTgyMzk3',
    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
   };

   var dataString = 'grant_type=authorization_code&code=' + req.query.code + '&redirect_uri=' + callbackurl + '';

   var options = {
    uri: 'https://my.url.com/oauth2/v1/token',
    method: 'POST',
    headers: headers,
    body: dataString,
    family: 4
   };

    function callback(error, response, body) {

    if (!error && response.statusCode == 200) {
     var jsonObj = JSON.parse(body);
     for (var myKey in jsonObj) {
      var value = jsonObj[myKey];
      console
       .log("key:" + myKey + ", value:" + value);
      if (myKey === 'access_token') {
       req.session.accesstoken = value;
       console
        .log('Setting Access Token into session-' + req.session.accesstoken);
      }
     }
     res.render('index.jade');
    } else {
      console.error(error);
     // console.log(response);
     res
      .render(
       'error', {
        'title': 'Authorization error',
        'message': 'Error occured during authorization. Please check with your identity service provider'
       });
    }
   }


   request(options, callback);

And my log says

 2017-07-26T03:06:30.701992+00:00 OrderManagement[web.2]: Express server listening on port 8080
2017-07-26T03:06:32.707668+00:00 OrderManagement[web.2]: #033[90mHEAD / #033[32m200 #033[90m449ms - 318b#033[0m
2017-07-26T03:06:40.022051+00:00 OrderManagement[web.2]: #033[90mGET / #033[32m200 #033[90m50ms - 318b#033[0m
2017-07-26T03:07:07.694066+00:00 OrderManagement[web.2]: #033[90mGET / #033[32m200 #033[90m42ms - 318b#033[0m
2017-07-26T03:07:12.098025+00:00 OrderManagement[web.2]: #033[90mGET / #033[32m200 #033[90m34ms - 318b#033[0m
2017-07-26T03:07:12.226770+00:00 OrderManagement[web.2]: #033[90mGET /stylesheets/style.css #033[32m200 #033[90m10ms - 117b#033[0m
2017-07-26T03:07:14.343551+00:00 OrderManagement[web.2]: #033[90mGET /auth #033[36m302 #033[90m13ms - 560b#033[0m
2017-07-26T03:07:18.359009+00:00 OrderManagement[web.2]: #033[90mGET / #033[32m200 #033[90m40ms - 318b#033[0m
2017-07-26T03:07:22.938992+00:00 OrderManagement[web.2]: inside callback
2017-07-26T03:07:22.939374+00:00 OrderManagement[web.2]: Authorization code is - AQIDBAW95eO2ziGpFSfOEzEd9tMzYpYpGV7zL3-4arEwhe-JWEU32lG0BKQNdnrUE1EJRFWW4kwnunMQi84To7_E8u3mMTEgRU5DUllQVElPTl9LRVkxNCB7djF9NCA=
2017-07-26T03:07:22.939664+00:00 OrderManagement[web.2]: Get Access Token and Validate it. If valid then render the order page or show error page
2017-07-26T03:07:23.009412+00:00 OrderManagement[web.2]: { Error: getaddrinfo ENOTFOUND my.url.com:8943
2017-07-26T03:07:23.009756+00:00 OrderManagement[web.2]:     at errnoException (dns.js:28:10)
2017-07-26T03:07:23.010029+00:00 OrderManagement[web.2]:     at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
2017-07-26T03:07:23.010332+00:00 OrderManagement[web.2]:   code: 'ENOTFOUND',
2017-07-26T03:07:23.010613+00:00 OrderManagement[web.2]:   errno: 'ENOTFOUND',
2017-07-26T03:07:23.010870+00:00 OrderManagement[web.2]:   syscall: 'getaddrinfo',
2017-07-26T03:07:23.011126+00:00 OrderManagement[web.2]:   hostname: 'my.url.com',
2017-07-26T03:07:23.011425+00:00 OrderManagement[web.2]:   host: 'my.url.com',
2017-07-26T03:07:23.011680+00:00 OrderManagement[web.2]:   port: '8943' }
2017-07-26T03:07:23.054720+00:00 OrderManagement[web.2]: #033[90mGET /callback?code=AQIDBAW95eO2ziGpFSfOEzEd9tMzYpYpGV7zL3-4arEwhe-JWEU32lG0BKQNdnrUE1EJRFWW4kwnunMQi84To7_E8u3mMTEgRU5DUllQVElPTl9LRVkxNCB7djF9NCA=&state=23456 #033[32m200 #033[90m123ms - 316b#033[0m
2017-07-26T03:07:23.204151+00:00 OrderManagement[web.2]: #033[90mGET /stylesheets/style.css #033[36m304 #033[90m2ms#033[0m
2017-07-26T03:07:38.207878+00:00 OrderManagement[web.2]: #033[90mGET / #033[32m200 #033[90m36ms - 318b#033[0m

I am not having OS level access to the cloud provider. So I cannot add the same DNS entry when i host it in the cloud provider. The URI i am trying to hit is a public URI.

juniorbansal
  • 1,249
  • 8
  • 31
  • 51
  • I think you're supposed to use `path` or `host` and not `uri` there – Sterling Archer Jul 26 '17 at 16:13
  • Possible duplicate of [Node.js getaddrinfo ENOTFOUND](https://stackoverflow.com/questions/17690803/node-js-getaddrinfo-enotfound) – Sterling Archer Jul 26 '17 at 16:13
  • No its not a duplicate of the other one.The other one uses http.get. But mine is post request and i am using the node's request library. – juniorbansal Jul 26 '17 at 16:14
  • The url you are trying to use may be wrong, try using CURL or postman to send the POST to the url. If that doesn't work then you know the problem is the URL. – 88jayto Jul 26 '17 at 16:42
  • it works perfectly fine when i test it on my local machine. The problem is there only when i deploy in my cloud provider – juniorbansal Jul 26 '17 at 16:53
  • The cloud provider could have a firewall rule blocking you, I would check the firewall rules. If you can, SSH into the cloud machine and try to do the CURL from there to verify the cloud machine has access to the url. – 88jayto Jul 26 '17 at 16:55

0 Answers0