kobim
kobim

Reputation: 33

Get http status code in node.js

So, i'm trying to get the http status code of an URL that i pass to the funcion. so far i've used the request module but the problem is that request fetches all the content of the given URL and if the content is a big amount of data it's slow, but i just need to get the status code to make an if statement.

request(audioURL, function (error, response) {
    if (!error && response.statusCode == 200) {
        queue.push({
            title: videoTitle, 
            user: message.author.username, 
            mention: message.author.mention(), 
            url: audioURL
        });

        bot.reply(message, "\"" + videoTitle + "\" has been added to the queue.")
    }
    else {
        bot.reply(message, "Sorry, I couldn't get the audio track for that video. HTTP status code: " + response.statusCode);
    }
});

This is what i got so far.audioURL is a basic string with a link to a media file

Upvotes: 0

Views: 7607

Answers (1)

user4454229
user4454229

Reputation:

http.request can take an options Object as its first parameter. Use something like this:

const options = {
    method: 'HEAD',
    host: 'example.com',
};

const req = http.request(options, (res) => {
    console.log(JSON.stringify(res.headers));
});

req.end();

The problem is that you'll want to turn your audioURL into its components, to pass as parameters of options. I'm using the HEAD method, that fetches only the headers of your request.

Upvotes: 2

Related Questions