2

nodejs check url is exist but don't download it

I found the npm packages urlExists http, they make a request and response with the body data.

like the command curl -i url

I want don't download the body data and know the url is exist.

flyflydogdog
  • 307
  • 3
  • 10

1 Answers1

3

You can use the HEAD HTTP method:

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response.

RFC2616

For example:

const fetch = require('node-fetch');

const response = await fetch('https://google.com', {
    method: 'HEAD'
});

console.log(response.ok);

Of course this would rely on the web server properly implementing the RFC.

Louis Matthijssen
  • 516
  • 1
  • 6
  • 18