简体   繁体   中英

How to use dns.lookup() within POST method?

I work on the URL shortener project. I want to use dns.lookup method. It works for me while using it separately. Once I use it within POST method, it does not work anymore and returns false even though an address is valid. Could you help me with it? Thank you! Here is the whole project: https://url-shortener-lm.glitch.me Here is the problem code:

app.post('/api/shorturl/new', (req, res, next) => {
  var originalURL = req.body.url;
  dns.lookup(originalURL, (err, address, family) => {
    if (err) {
      res.json({
        originalURL: originalURL,
        shortenedURL: "Invalid URL"
      });
    } else {
      var shortenedURL = Math.floor(Math.random() * 100000).toString();

      var data = new Model({
        originalURL: originalURL,
        shortenedURL: shortenedURL
      });

      data.save(function(err, data) {
        if (err) {
          return console.error(err);
        }
      });

      res.json({
        originalURL: originalURL,
        shortenedURL: shortenedURL
      })
    };
  });
});

dns.lookup() expects you to pass it only a hostname such as www.google.com , not a url. You can see a code example in the doc where it just passes "example.com" .

You can parse the hostname out of the url. You can either use the original node.js url parsing capabilities or the newer standard URL object (that is common to both browser and node.js). I show the newer URL object here:

const URL = require('url').URL;

app.post('/api/shorturl/new', (req, res, next) => {

  const originalURL = req.body.url;
  const urlObject = new URL(originalURL);

  dns.lookup(urlObject.hostname, (err, address, family) => {
    if (err) {
      res.json({
        originalURL: originalURL,
        shortenedURL: "Invalid URL"
      });
    } else {
      var shortenedURL = Math.floor(Math.random() * 100000).toString();

      var data = new Model({
        originalURL: originalURL,
        shortenedURL: shortenedURL
      });

      data.save(function(err, data) {
        if (err) {
          return console.error(err);
        }
      });

      res.json({
        originalURL: originalURL,
        shortenedURL: shortenedURL
      })
    };
  });
});

To see what the URL object is capable of parsing out of a URL such as:

http://www.example.com:3000/somePath/somePage.html

Here's a simple test app and the output that it delivers:

const {URL} = require('url');

let originalURL = "http://www.example.com:3000/somePath/somePage.html"
const urlObject = new URL(originalURL);

console.log(urlObject.hostname);
console.log(urlObject);

That shows this output:

www.example.com
URL {
  href: 'http://www.example.com:3000/somePath/somePage.html',
  origin: 'http://www.example.com:3000',
  protocol: 'http:',
  username: '',
  password: '',
  host: 'www.example.com:3000',
  hostname: 'www.example.com',
  port: '3000',
  pathname: '/somePath/somePage.html',
  search: '',
  searchParams: URLSearchParams {},
  hash: ''
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM