Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js check if a remote URL exists

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.

app.get('/api/v1/urlCheck/', function (req,res) {
    var url=req.query['url'];
    var request = require('request');
    request.get(url, {timeout: 30000, json:false}, function (error, result) {
        res.send(result.body);

    });

});

Appreciate any help!

like image 779
metalaureate Avatar asked Sep 24 '14 01:09

metalaureate


4 Answers

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();
like image 70
danwarfel Avatar answered Nov 01 '22 04:11

danwarfel


2021 update

Use url-exist:

import urlExist from 'url-exist';

const exists = await urlExist('https://google.com');

// Handle result
console.log(exists);

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.

like image 22
Richie Bendall Avatar answered Nov 01 '22 02:11

Richie Bendall


Thanks! Here it is, encapsulated in a function (updated on 5/30/17 with the require outside):

    var http = require('http'),
         url = require('url');

    exports.checkUrlExists = function (Url, callback) {
        var options = {
            method: 'HEAD',
            host: url.parse(Url).host,
            port: 80,
            path: url.parse(Url).pathname
        };
        var req = http.request(options, function (r) {
            callback( r.statusCode== 200);});
        req.end();
    }

It's very quick (I get about 50 ms, but it will depend on your connection and the server speed). Note that it's also quite basic, i.e. it won't handle redirects very well...

like image 18
Nico Avatar answered Nov 01 '22 03:11

Nico


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 
});
like image 9
Rakesh Soni Avatar answered Nov 01 '22 04:11

Rakesh Soni