node.js check if a remote URL exists

前端 未结 11 760
萌比男神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:50

    I see in your code that you are already using the request library, so just:

    const request = require('request');
    
    request.head('http://...', (error, res) => {
      const exists = !error && res.statusCode === 200;
    });
    

提交回复
热议问题