1

When I use a component like

const React = require('react');
const dns = require('dns');

class DnsResolver extends React.Component {
    componentDidMount() {
        dns.resolve('https://www.google.com', (err, addresses) => {
            this.setState({
                address: addresses
            });
        });
    }

    render() {
        return (
            <div>
                {this.state.address}
            </div>
        );
    }
}

module.exports = DnsResolver;

the result is 0.0.0.0. Somehow, the browser cannot resolve the address. Why might this happen?

5
  • 4
    You cannot use DNS from a browser. Commented May 20, 2019 at 13:53
  • Says where? Then what is the dns.resolve() function for? reference Commented May 20, 2019 at 14:34
  • 1
    That's the web extensions API, not in-page JavaScript Commented May 20, 2019 at 15:09
  • The initial point of my problem started when I tried to use mongodb npm module. It uses dns npm module to resolve the mongodb address, but since it runs at the frontend, dns.resolve() translates into this web extension function. NodeJS dns returns an array but web extension dns may return string. That's where the trouble starts. I maybe should not use mongodb at the frontend. Commented May 20, 2019 at 17:13
  • 1
    You cannot use MongoDB from the browser either. You need a backend. Commented May 20, 2019 at 17:18

1 Answer 1

2

dns is a native NodeJS module, it is intended to use in the server.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.