node.js check if a remote URL exists

前端 未结 11 759
萌比男神i
萌比男神i 2021-02-05 04:28

How do I check to see if a URL exists without pulling it down? I use the following code, but it downloads the whole file. I just need to check that it exists.

ap         


        
相关标签:
11条回答
  • 2021-02-05 04:37

    Simply use url-exists npm package to test if url exists or not

    var urlExists = require('url-exists');
    
    urlExists('https://www.google.com', function(err, exists) {
      console.log(exists); // true 
    });
    
    urlExists('https://www.fakeurl.notreal', function(err, exists) {
      console.log(exists); // false 
    });
    
    0 讨论(0)
  • 2021-02-05 04:40

    2020 update

    request has now been deprecated which has brought down url-exists with it. Use url-exist instead.

    const urlExist = require("url-exist");
    
    (async () => {
        const exists = await urlExist("https://google.com");
        // Handle result
        console.log(exists)
    })();
    

    If you (for some reason) need to use it synchronously, you can use url-exist-sync.

    2019 update

    Since 2017, request and callback-style functions (from url-exists) have fallen out of use.

    However, there is a fix. Swap url-exists for url-exist.

    So instead of using:

    const urlExists = require("url-exists")
    
    urlExists("https://google.com", (_, exists) => {
        // Handle result
        console.log(exists)
    })
    

    Use this:

    const urlExist = require("url-exist");
    
    (async () => {
        const exists = await urlExist("https://google.com");
        // Handle result
        console.log(exists)
    })();
    

    Original answer (2017)

    If you have access to the request package, you can try this:

    const request = require("request")
    const urlExists = url => new Promise((resolve, reject) => request.head(url).on("response", res => resolve(res.statusCode.toString()[0] === "2")))
    urlExists("https://google.com").then(exists => console.log(exists)) // true
    

    Most of this logic is already provided by url-exists.

    0 讨论(0)
  • 2021-02-05 04:41

    Try this:

    var http = require('http'),
        options = {method: 'HEAD', host: 'stackoverflow.com', port: 80, path: '/'},
        req = http.request(options, function(r) {
            console.log(JSON.stringify(r.headers));
        });
    req.end();
    
    0 讨论(0)
  • 2021-02-05 04:45

    Take a look at the url-exists npm package https://www.npmjs.com/package/url-exists

    Setting up:

    $ npm install url-exists
    

    Useage:

    const urlExists = require('url-exists');
    
    urlExists('https://www.google.com', function(err, exists) {
      console.log(exists); // true 
    });
    
    urlExists('https://www.fakeurl.notreal', function(err, exists) {
      console.log(exists); // false 
    });
    

    You can also promisify it to take advantage of await and async:

    const util = require('util');
    const urlExists = util.promisify(require('url-exists'));
    
    let isExists = await urlExists('https://www.google.com'); // true
    isExists = await urlExists('https://www.fakeurl.notreal'); // false
    

    Happy coding!

    0 讨论(0)
  • 2021-02-05 04:46

    Using the other responses as reference, here's a promisified version which also works with https uris (for node 6+):

    const http = require('http');
    const https = require('https');
    const url = require('url');
    
    const request = (opts = {}, cb) => {
      const requester = opts.protocol === 'https:' ? https : http;
      return requester.request(opts, cb);
    };
    
    module.exports = target => new Promise((resolve, reject) => {
      let uri;
    
      try {
        uri = url.parse(target);
      } catch (err) {
        reject(new Error(`Invalid url ${target}`));
      }
    
      const options = {
        method: 'HEAD',
        host: uri.host,
        protocol: uri.protocol,
        port: uri.port,
        path: uri.path,
        timeout: 5 * 1000,
      };
    
      const req = request(options, (res) => {
        const { statusCode } = res;
    
        if (statusCode >= 200 && statusCode < 300) {
          resolve(target);
        } else {
          reject(new Error(`Url ${target} not found.`));
        }
      });
    
      req.on('error', reject);
    
      req.end();
    });
    

    It can be used like this:

    const urlExists = require('./url-exists')
    
    urlExists('https://www.google.com')
      .then(() => {
        console.log('Google exists!');
      })
      .catch(() => {
        console.error('Invalid url :(');
      });
    
    0 讨论(0)
  • 2021-02-05 04:47

    require into functions is wrong way in Node. Followed ES6 method supports all correct http statuses and of course retrieve error if you have a bad 'host' like fff.kkk

    checkUrlExists(host,cb) {
        http.request({method:'HEAD',host,port:80,path: '/'}, (r) => {
            cb(null, r.statusCode >= 200 && r.statusCode < 400 );
        }).on('error', cb).end();
    }
    
    0 讨论(0)
提交回复
热议问题