node.js express dns lookup to use ipv4 always

1.7k views Asked by At

Is there a way to have the express.js server always use do a dns look up for IPV4 addresses instead of both IPv4 and IPV6 addresses?

Like we do this in a java JVM by setting the parameter java.net.preferIPv4Stack=true

I am looking to do this for outgoing requests, when my add is making outbound service calls. My app uses request npm package and it uses net npm package and it uses dns npm package. The dns package has an option to set the IP family which can be 4 or 6. I think this will do what i want if i set it to 4.

Now the question is, how to pass this option from my app to be applied to this npm package at runtime.

1

There are 1 answers

0
mandy On BEST ANSWER

I had to do this to force the DNS look up to be looking up only IPv4 addresses and not IPv6 addresses.

import Request from 'request';

export class SoapUtility extends Utility 
{

  constructor(options) {
        super();
        otherCode = otherCode;

        this.request = Request;
  };

  requestMainframe(args, callback) 
  {
    this.request.post({
        url: this.endpoints.userLookup,
        method: 'POST',
        family : 4,
        headers: {
            'Content-Type': 'text/xml',
            "Authorization": `Basic ${creds}`
        },
        body: requestBody,
        rejectUnauthorized: false, 
    }, (error, response, body) => {
          callback(error, null);
    });
  }
}

Here is the documentation from npm request and DNS lookup details